0% found this document useful (0 votes)
62 views

Socket

The document contains code for a server and client program that communicate over sockets using TCP and UDP. The Server2 class creates a server socket to listen for incoming client connections on port 888. It reads data received from the client and sends responses back. The Client2 class connects to the server socket, sends data to the server, and receives responses back. It continues this send/receive loop until the client types "exit". The UDPEchoServer and UDPEchoClient classes demonstrate a simple UDP client-server model where the server echoes back any data packets it receives from the client.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Socket

The document contains code for a server and client program that communicate over sockets using TCP and UDP. The Server2 class creates a server socket to listen for incoming client connections on port 888. It reads data received from the client and sends responses back. The Client2 class connects to the server socket, sends data to the server, and receives responses back. It continues this send/receive loop until the client types "exit". The UDPEchoServer and UDPEchoClient classes demonstrate a simple UDP client-server model where the server echoes back any data packets it receives from the client.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

// Server2 class that

// receives data and sends data


import java.io.*;
import java.net.*;
class Server2 {
public static void main(String args[])
throws Exception
{
// Create server Socket
ServerSocket ss = new ServerSocket(888);

// connect it to client socket


Socket s = ss.accept();
System.out.println("Connection established");

// to send data to the client


PrintStream ps = new PrintStream(s.getOutputStream());

// to read data coming from the client


BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));

// to read data from the keyboard


BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));

// server executes continuously


while (true) {

String str, str1;

// repeat as long as the client


// does not send a null string

// read from client


while ((str = br.readLine()) != null) {
System.out.println(str);
str1 = kb.readLine();

// send to client
ps.println(str1);
}

// close connection
ps.close();
br.close();
kb.close();
ss.close();
s.close();

// terminate application
System.exit(0);
} // end of while
}
}
// Client2 class that
// sends data and receives also

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

class Client2 {

public static void main(String args[])


throws Exception
{

// Create client socket


Socket s = new Socket("localhost", 888);

// to send data to the server


DataOutputStream dos
= new DataOutputStream(
s.getOutputStream());

// to read data coming from the server


BufferedReader br
= new BufferedReader(
new InputStreamReader(
s.getInputStream()));

// to read data from the keyboard


BufferedReader kb
= new BufferedReader(
new InputStreamReader(System.in));
String str, str1;

// repeat as long as exit


// is not typed at client
while (!(str = kb.readLine()).equals("exit")) {

// send to the server


dos.writeBytes(str + "\n");

// receive from the server


str1 = br.readLine();

System.out.println(str1);
}

// close connection.
dos.close();
br.close();
kb.close();
s.close();
}
}
import java.net.*;
import java.io.*;
public class UDPEchoServer
{
public static void main(String args[]) throws SocketException,
IOException
{
DatagramSocket aSocket = new DatagramSocket(2000);
try
{
byte[] buffer = new byte[1000];
while(true)
{
DatagramPacket request = new
DatagramPacket(buffer,buffer.length);
aSocket.receive(request);
DatagramPacket reply = new
DatagramPacket(request.getData(),request.getLength(),request.getAddress(),req
uest.getPort());
aSocket.send(reply);
}
}
finally
{
if (aSocket != null)
aSocket.close();
}
}
}
import java.net.*;
import java.io.*;
public class UDPEchoClient
{
public static void main(String args[]) throws SocketException,
IOException
{
DatagramSocket aSocket = new DatagramSocket();
try
{
String m = "Hello UDPEchoServer";
InetAddress aHost = InetAddress.getLocalHost();
DatagramPacket request = new DatagramPacket(m.getBytes(),
m.length(), aHost, 2000);
aSocket.send(request);
byte[] buffer = new byte[1000];
DatagramPacket reply = new DatagramPacket(buffer,
buffer.length);
aSocket.receive(reply);
System.out.println("Reply: " + new
String(reply.getData()).trim());
}
finally
{
if (aSocket != null)
aSocket.close();
}
}
}

You might also like