Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communication between client and server side based java #401

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions Dynamic Programming/Thread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package multitrycatch;

class Point {
int first, second;

public Point(int first, int second) {
this.first = first;
this.second = second;
}
}

class MaxSumSubMatrix
{
// function to find maximum sum k x k sub-matrix
public static Point findMaxSumSubMatrix(int mat[][], int k)
{
// M x N matrix
int M = mat.length;
int N = mat[0].length;

// pre-process the input matrix such that sum[i][j] stores
// sum of elements in matrix from (0, 0) to (i, j)
int[][] sum = new int[M][N];
sum[0][0] = mat[0][0];

// pre-process first row
for (int j = 1; j < N; j++) {
sum[0][j] = mat[0][j] + sum[0][j - 1];
}

// pre-process first column
for (int i = 1; i < M; i++) {
sum[i][0] = mat[i][0] + sum[i - 1][0];
}

// pre-process rest of the matrix
for (int i = 1; i < M; i++) {
for (int j = 1; j < N; j++) {
sum[i][j] = mat[i][j] + sum[i - 1][j] + sum[i][j - 1]
- sum[i - 1][j - 1];
}
}

int total, max = Integer.MIN_VALUE;
Point p = null;

// find maximum sum sub-matrix

// start from cell (k - 1, k - 1) and consider each
// submatrix of size k x k
for (int i = k - 1; i < M; i++)
{
for (int j = k - 1; j < N; j++)
{
// Note (i, j) is bottom right corner coordinates of
// square sub-matrix of size k

total = sum[i][j];
if (i - k >= 0) {
total = total - sum[i - k][j];
}

if (j - k >= 0) {
total = total - sum[i][j - k];
}

if (i - k >= 0 && j - k >= 0) {
total = total + sum[i - k][j - k];
}

if (total > max) {
max = total;
p = new Point(i, j);
}
}
}

// returns coordinates of bottom right corner of sub-matrix
return p;
}

public static void main(String[] args)
{
// 5 x 5 matrix
int[][] mat =
{
{ 3, -4, 6, -5, 1 },
{ 1, -2, 8, -4, -2 },
{ 3, -8, 9, 3, 1 },
{ -7, 3, 4, 2, 7 },
{ -3, 7, -5, 7, -6 }
};

// sub-matrix size
int k = 3;

// p contains bottom right corner coordinates of sub-matrix
Point p = findMaxSumSubMatrix(mat, k);

// print maximum sum sub-matrix
for (int i = 0; i < k; i++)
{
for (int j = 0; j < k; j++)
{
int r = i + p.first - k + 1;
int c = j + p.second - k + 1;
System.out.printf("%3d", mat[r][c]);
}

System.out.println();
}
}
}

32 changes: 32 additions & 0 deletions Networking/Client -Machine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.io.*;
import java.net.*;
public class ClientMachine
{
public static void main(String[] args) throws Exception
{
Socket sock = new Socket("localhost", 3000);
//reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
//sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);

// receiving from server ( receiveRead object)
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

System.out.println("Start the chatting, type and press Enter key");

String receiveMessage, sendMessage;
while(true)
{
sendMessage = keyRead.readLine(); // keyboard reading
pwrite.println(sendMessage); // sending to server
pwrite.flush(); // flush the data
if((receiveMessage = receiveRead.readLine()) != null) //receive from server
{
System.out.println(receiveMessage); // displaying at DOS prompt
}
}
}
}
32 changes: 32 additions & 0 deletions Networking/Create ServerMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.io.*;
import java.net.*;
public class ServerMachine
{
public static void main(String[] args) throws Exception
{
ServerSocket sersock = new ServerSocket(3000);
System.out.println("Server ready for sending message...");
Socket sock = sersock.accept( );
//reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
//sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);

//receiving from server ( receiveRead object)
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

String receiveMessage, sendMessage;
while(true)
{
if((receiveMessage = receiveRead.readLine()) != null)
{
System.out.println(receiveMessage);
}
sendMessage = keyRead.readLine();
pwrite.println(sendMessage);
pwrite.flush();
}
}
}
38 changes: 38 additions & 0 deletions Thread_Programming/multiplethread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class MyThread implements Runnable {

String name;
Thread t;
MyThread (String thread){
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start();
}


public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
}catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}
}

class MultiThread {
public static void main(String args[]) {
new MyThread("One");
new MyThread("Two");
new NewThread("Three");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}