Skip to content

Commit 6e26dc1

Browse files
Server and Client files
1 parent 6667545 commit 6e26dc1

File tree

5 files changed

+305
-0
lines changed

5 files changed

+305
-0
lines changed

ClientSoc.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package clientsoc;
7+
8+
import java.util.*;
9+
import java.lang.*;
10+
import java.io.*;
11+
import java.net.*;
12+
/**
13+
*
14+
* @author jeevarajendran
15+
*/
16+
public class ClientSoc {
17+
18+
public static void main(String[] args) {
19+
// TODO code application logic here
20+
21+
Socket clientSoc = null;
22+
PrintStream output = null;
23+
24+
try
25+
{
26+
clientSoc = new Socket("localhost",9999);
27+
}
28+
catch(Exception e)
29+
{
30+
System.out.println(e + "Exception in client 1 connection");
31+
}
32+
33+
try
34+
{
35+
output = new PrintStream(clientSoc.getOutputStream());
36+
BufferedReader bd = new BufferedReader(new InputStreamReader(clientSoc.getInputStream()));
37+
while(true)
38+
{
39+
System.out.println("Enter your message to the server : ");
40+
InputStreamReader rd = new InputStreamReader(System.in);
41+
BufferedReader br = new BufferedReader(rd);
42+
String temp = br.readLine();
43+
output.println(temp);
44+
45+
String messagefromserver = bd.readLine();
46+
System.out.println("Message from Server to you : "+messagefromserver+" \n");
47+
}
48+
}
49+
catch(Exception e)
50+
{
51+
System.out.println(e + "Exception in client 1 message" );
52+
}
53+
}
54+
}
55+

ClientSoc2.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package clientsoc2;
7+
8+
import java.util.*;
9+
import java.lang.*;
10+
import java.io.*;
11+
import java.net.*;
12+
/**
13+
*
14+
* @author jeevarajendran
15+
*/
16+
public class ClientSoc2 {
17+
18+
public static void main(String[] args) {
19+
// TODO code application logic here
20+
21+
Socket clientSoc = null;
22+
PrintStream output = null;
23+
24+
try
25+
{
26+
clientSoc = new Socket("localhost",9999);
27+
}
28+
catch(Exception e)
29+
{
30+
System.out.println(e + "Exception in client 2 connection" );
31+
}
32+
33+
try
34+
{
35+
output = new PrintStream(clientSoc.getOutputStream());
36+
BufferedReader bd = new BufferedReader(new InputStreamReader(clientSoc.getInputStream()));
37+
while(true)
38+
{
39+
System.out.println("Enter Something (in client): ");
40+
InputStreamReader rd = new InputStreamReader(System.in);
41+
BufferedReader br = new BufferedReader(rd);
42+
String temp = br.readLine();
43+
output.println(temp);
44+
45+
String messagefromserver = bd.readLine();
46+
System.out.println("Message from Client : "+messagefromserver+" \n");
47+
}
48+
}
49+
catch(Exception e)
50+
{
51+
System.out.println(e + "Exception in client 2 message" );
52+
}
53+
}
54+
}
55+

ClientSoc3.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package clientsoc3;
7+
8+
import java.util.*;
9+
import java.lang.*;
10+
import java.io.*;
11+
import java.net.*;
12+
/**
13+
*
14+
* @author jeevarajendran
15+
*/
16+
public class ClientSoc3 {
17+
18+
public static void main(String[] args) {
19+
// TODO code application logic here
20+
21+
Socket clientSoc = null;
22+
PrintStream output = null;
23+
24+
try
25+
{
26+
clientSoc = new Socket("localhost",9999);
27+
}
28+
catch(Exception e)
29+
{
30+
System.out.println(e + "Exception in client 3 connection" );
31+
}
32+
33+
try
34+
{
35+
output = new PrintStream(clientSoc.getOutputStream());
36+
BufferedReader bd = new BufferedReader(new InputStreamReader(clientSoc.getInputStream()));
37+
while(true)
38+
{
39+
System.out.println("Enter Something (in client): ");
40+
InputStreamReader rd = new InputStreamReader(System.in);
41+
BufferedReader br = new BufferedReader(rd);
42+
String temp = br.readLine();
43+
output.println(temp);
44+
45+
String messagefromserver = bd.readLine();
46+
System.out.println("Message from Client : "+messagefromserver+" \n");
47+
}
48+
}
49+
catch(Exception e)
50+
{
51+
System.out.println(e + "Exception in client 3 message" );
52+
}
53+
}
54+
}
55+

ClientSoc4.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package clientsoc4;
7+
8+
import java.util.*;
9+
import java.lang.*;
10+
import java.io.*;
11+
import java.net.*;
12+
/**
13+
*
14+
* @author jeevarajendran
15+
*/
16+
public class ClientSoc4 {
17+
18+
public static void main(String[] args) {
19+
// TODO code application logic here
20+
21+
Socket clientSoc = null;
22+
PrintStream output = null;
23+
24+
try
25+
{
26+
clientSoc = new Socket("localhost",9999);
27+
}
28+
catch(Exception e)
29+
{
30+
System.out.println(e);
31+
}
32+
33+
try
34+
{
35+
output = new PrintStream(clientSoc.getOutputStream());
36+
BufferedReader bd = new BufferedReader(new InputStreamReader(clientSoc.getInputStream()));
37+
while(true)
38+
{
39+
System.out.println("Enter Something (in client): ");
40+
InputStreamReader rd = new InputStreamReader(System.in);
41+
BufferedReader br = new BufferedReader(rd);
42+
String temp = br.readLine();
43+
output.println(temp);
44+
45+
String messagefromserver = bd.readLine();
46+
System.out.println("Message from Client : "+messagefromserver+" \n");
47+
}
48+
}
49+
catch(Exception e)
50+
{
51+
System.out.println(e);
52+
}
53+
}
54+
}
55+

ServerSoc.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package serversoc;
7+
8+
import java.io.*;
9+
import java.net.*;
10+
import java.util.concurrent.ExecutorService;
11+
import java.util.concurrent.Executors;
12+
13+
/**
14+
*
15+
* @author jeevarajendran
16+
*/
17+
public class ServerSoc implements Runnable {
18+
19+
/**
20+
* @param args the command line arguments
21+
*/
22+
Socket clientSocket;
23+
ServerSoc(Socket clientSocket)
24+
{
25+
this.clientSocket = clientSocket;
26+
}
27+
public static void main(String[] args) {
28+
29+
ServerSocket serSoc = null;
30+
31+
try
32+
{
33+
System.out.println("Server running :\n");
34+
serSoc = new ServerSocket(9999);
35+
}
36+
catch(Exception e)
37+
{
38+
System.out.println(e + ": Exception in server");
39+
}
40+
41+
ExecutorService pool = Executors.newFixedThreadPool(2);
42+
43+
try
44+
{
45+
while(true)
46+
{
47+
System.out.println("Waiting for a client to get connected :\n");
48+
System.out.println("------------------------------------------\n");
49+
Socket socket = serSoc.accept();
50+
pool.execute(new Thread(new ServerSoc(socket)));
51+
}
52+
53+
}
54+
catch(Exception e)
55+
{
56+
System.out.println(e + "Exception while waiting for the client");
57+
}
58+
}
59+
public void run()
60+
{
61+
PrintStream output = null;
62+
try
63+
{
64+
System.out.println("Connected to a client : \n ");
65+
System.out.println("---------------------------- \n ");
66+
BufferedReader bd = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
67+
output = new PrintStream(clientSocket.getOutputStream());
68+
while(true)
69+
{
70+
String temp = bd.readLine();
71+
System.out.println("Message from Client : "+ temp +" \n");
72+
System.out.println("Enter your message for the client : ");
73+
InputStreamReader rd = new InputStreamReader(System.in);
74+
BufferedReader br = new BufferedReader(rd);
75+
String messagetoclient = br.readLine();
76+
output.println(messagetoclient);
77+
}
78+
}
79+
catch(Exception e)
80+
{
81+
System.out.println(e);
82+
}
83+
}
84+
}
85+

0 commit comments

Comments
 (0)