You are on page 1of 9

1 AIM Socket Programming implement TCP client and server socket

In this practical, we will learn how to create a simple Server and clients that connects to each other with
Sockets over TCP using java programming language. To use Java Programming language, you need to
install the Java Development Kit ( JDK ) as well as a programming editor ( IDE ) such as Eclipse. For more
details of how to install JDK and Eclipse, Use the following practical:
Java Programming : Creating a simple java project using Eclipse
Two sections as follows:
o Creating and Running a Server
o Creating a Java Client.
A. Creating and Running a Server
1 Let’s create a new project using Eclipse
: SocketHelloServer -> Click Finish once done

2 Within the new project: Right Click on the project name within the Package Explorer,
Choose, New then Class
3 Type in the name of the class as : Server Then click Finish
4 We write the code for the Server that does the Addition operation. Copy the following code into
the Server class that we had just created.
1. import java.io.*;
2. import java.net.*;
3.
4. class Server
5. {
6. public static void main(String argv[]) throws Exception
7. {
8.
9. System.out.println(" Server is Running " );
10. ServerSocket mysocket = new ServerSocket(5555);
11.
12. while(true)
13. {
14. Socket connectionSocket = mysocket.accept();
15.
16. BufferedReader reader =
17. new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
18. BufferedWriter writer=
19. new BufferedWriter(new
OutputStreamWriter(connectionSocket.getOutputStream()));
20.
21. writer.write("*** Welcome to the Calculation Server (Addition Only) ***\r\n");
22. writer.write("*** Please type in the first number and press Enter : \n");
23. writer.flush();
24. String data1 = reader.readLine().trim();
25.
26. writer.write("*** Please type in the second number and press Enter : \n");
27. writer.flush();
28. String data2 = reader.readLine().trim();
29.
30. int num1=Integer.parseInt(data1);
31. int num2=Integer.parseInt(data2);
32.
33. int result=num1+num2;
34. System.out.println("Addition operation done " );
35.
36. writer.write("\r\n=== Result is : "+result);
37. writer.flush();
38. connectionSocket.close();
39. }
40. }
41. }
5 Compile and Run the Application now. (Usually through clicking the green arrow on Eclipse as shown
below).

B. Creating a Java Client


Instead of using PuTTY as a client for the Addition Server, we can write Java Client through the following
steps.
1 Within Eclipse, Create NEW Java Project under the name : SocketHelloClient

2 Right Click on the project name SocketHelloClient inside the Package Explorer, choose New -
> Class. Type the name of the class as Client

3 Copy the following code the Client class. The Client is programmed to send two numbers: 8 and 10 to
the server for the Addition operation.
1. import java.io.*;
2. import java.net.*;
3. import java.util.*;
4.
5. public class Client {
6.
7. public static void main(String argv[])
8. {
9. try{
10. Socket socketClient= new Socket("localhost",5555);
11. System.out.println("Client: "+"Connection Established");
12.
13. BufferedReader reader =
14. new BufferedReader(new
InputStreamReader(socketClient.getInputStream()));
15.
16. BufferedWriter writer=
17. new BufferedWriter(new
OutputStreamWriter(socketClient.getOutputStream()));
18. String serverMsg;
19. writer.write("8\r\n");
20. writer.write("10\r\n");
21. writer.flush();
22. while((serverMsg = reader.readLine()) != null){
23. System.out.println("Client: " + serverMsg);
24. }
25.
26. }catch(Exception e){e.printStackTrace();}
27. }
28. }
4 Make sure that the server is running!
5 Run the client now. You will get the following into the console:
2 AIM implement client using UDP socket

import java.net.*;
import java.io.*;
public class client {
public static void main(String arg[])throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
DatagramSocket cs=new DatagramSocket();
InetAddress ip=InetAddress.getByName("localhost");
byte[] sendData=new byte[1024];
byte[] receiveData=new byte[1024];
int a[] = new int[5];
String str=br.readLine();
sendData=str.getBytes();
DatagramPacket sp=new DatagramPacket(sendData,sendData.length,ip,4222);
cs.send(sp);
DatagramPacket rp=new DatagramPacket(receiveData,receiveData.length);
cs.receive(rp);
String msg=new String(rp.getData());
System.out.println("From Server : "+msg);
cs.close();
}
}

import java.net.*;
public class server {
public static void main(String arg[])throws Exception{
DatagramSocket ss=new DatagramSocket(4222);
byte[] receiveData=new byte[1024];
byte[] sendData=new byte[1024];
while(true){
DatagramPacket rp=new DatagramPacket
(receiveData,receiveData.length);
ss.receive(rp);
String str=new String(rp.getData());
System.out.println("Received : "+str);
InetAddress ip=rp.getAddress();
int port=rp.getPort();
String cs=str.toUpperCase();
sendData=cs.getBytes();
DatagramPacket sp=new
DatagramPacket(sendData,sendData.length,ip,port);
ss.send(sp);
}
}
}

3 AIM File transfer using socket


FileCleint.java
import java.io.*;
import java.net.*;

class FileClient
{
static public void main(String a[]) throws Exception
{
Socket s=new Socket("localhost",786);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter u r File name:");
br.readLine();
DataOutputStream out=new DataOutputStream(s.getOutputStream());
out.writeBytes(br+"\n");
BufferedReader in=new BufferedReader(new
InputStreamReader(s.getInputStream()));
String str=in.readLine();
System.out.print(str);
if(str.equals("yes"))
{
while((str=in.readLine())!=null)
{
System.out.print(str);
}
br.close();
out.close();
in.close();
s.close(); }
else
{
System.out.println("File Not Found");
}
}}

Fileserver.java
import java.io.*;
import java.net.*;

class FileServer
{
static public void main(String ar[]) throws Exception
{
ServerSocket ss= new ServerSocket(786);
Socket s=ss.accept();
System.out.println("Connection is established by Soban Imam");
BufferedReader in=new BufferedReader(new
InputStreamReader(s.getInputStream()));
String fname=in.readLine();
System.out.println("You file name is:"+ fname);
FileReader fr=null;
BufferedReader file=null;
int flag=1;
DataOutputStream out=new DataOutputStream(s.getOutputStream());
File f=new File(fname);
if(f.exists())
{
out.writeBytes("yes"+"\n");
flag=1;

}
else
{
flag=0;
out.writeBytes("no"+"\n");
}
if(flag==1)
{
fr=new FileReader(fname);
file=new BufferedReader(fr);
String str;
while((str=file.readLine())!=null)
{
out.writeBytes(str+"\n");
}
file.close();
out.close();
in.close();
s.close();
ss.close();
} } }