Monday, February 22, 2010

Sample TCP Client & Server in Java

Sample Client/Server Application written in Java. Very simple, and just illustrates a very simple TCP based Server/Client Application that sends messages back and forth via a port on the server. This was a project for my Graduate School Class but thought it shared some interesting principles and showed the fundamental intricacies of this concept.

D:\Users\Amit Apollo Barman.DS\Desktop\JavaTCP\clienttcp.java
1 import java.io.*;
2 import java.net.*;
3
4 class clienttcp
5 {
6 public static void main(String argv[]) throws Exception
7 {
8 String magicWord="not exit";
9 String magicWordServer;
10 String HostName;
11 int portnum=2222;
12
13 while(magicWord!="exit")
14 {
15 System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
16 BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
17
18 System.out.println("Please enter the server name or host name:");
19 HostName = inFromUser.readLine();
20 System.out.println("Please enter your name to text to the server:");
21 magicWord = inFromUser.readLine();
22 System.out.println("Please enter the port:");
23
24 try
25 {
26 String portn = inFromUser.readLine();
27 portnum = Integer.parseInt(portn.trim());
28 Socket clientSocket = new Socket(HostName, portnum);
29 DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
30 BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
31 outToServer.writeBytes(magicWord + '\n');
32 magicWordServer = inFromServer.readLine();
33 System.out.println("FROM SERVER: " + magicWordServer);
34
35 System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
36 clientSocket.close();
37
38 }
39 catch (NumberFormatException nfe)
40 {
41 System.out.println("Number Format Exception: " + nfe.getMessage());
42 }
43
44
45 }
46 }
47 }
48
2/17/2010 clienttcp.java
D:/Users/…/clienttcp.html 1/1

D:\Users\Amit Apollo Barman.DS\Desktop\JavaTCP\servertcp.java
1 import java.io.*;
2 import java.net.*;
3
4 class servertcp
5 {
6 public static void main(String argv[]) throws Exception
7 {
8 String clientSentence;
9 String BackSentence="Not Exit";
10 int portnum=2222;
11
12 BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
13
14 System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
15
16 System.out.println("Please enter a port to listen on:");
17
18 try
19 {
20 String portn = inFromUser.readLine();
21 portnum = Integer.parseInt(portn.trim());
22
23 ServerSocket welcomeSocket = new ServerSocket(portnum);
24 System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
25
26 System.out.println("Listening on Port:" + portnum + "...");
27
28 while(BackSentence!="exit")
29 {
30 Socket connectionSocket = welcomeSocket.accept();
31 BufferedReader inFromClient =
32 new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
33 DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
34 clientSentence = inFromClient.readLine();
35 System.out.println("Received from Client: " + clientSentence);
36 BackSentence = clientSentence + '\n';
37 outToClient.writeBytes(BackSentence);
38 }
39
40 }
41 catch (NumberFormatException nfe)
42 {
43 System.out.println("Number Format Exception: " + nfe.getMessage());
44 }
45
46
47 System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
48
49 }
50 }
51
2/17/2010 servertcp.java
D:/Users/…/servertcp.html 1/1

No comments:

Post a Comment