0% found this document useful (0 votes)
26 views5 pages

Untitled

The document contains code for implementing client-server communication using TCP and UDP sockets in Java. For TCP, it includes Server.java and Client.java code that creates a server socket listening on port 5555 and a client socket connecting to localhost on that port to send and receive a string message. For UDP, it includes UDPServer.java and UDPClient.java code that uses DatagramSockets to send and receive string messages between a server on port 790 and client on port 789.

Uploaded by

Elijah Ibsa
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
26 views5 pages

Untitled

The document contains code for implementing client-server communication using TCP and UDP sockets in Java. For TCP, it includes Server.java and Client.java code that creates a server socket listening on port 5555 and a client socket connecting to localhost on that port to send and receive a string message. For UDP, it includes UDPServer.java and UDPClient.java code that uses DatagramSockets to send and receive string messages between a server on port 790 and client on port 789.

Uploaded by

Elijah Ibsa
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 5

MAU 4th Year CS

Your answer

Classmate answers

Selamawit Alemu

Server.java

import java.io.*;

import java.net.*;

public class Server {

public static void main(String[] args){

try

ServerSocket server=new ServerSocket(5555);

Socket s=server.accept();

DataInputStream din=new DataInputStream(s.getInputStream());

String str=(String)din.readUTF();

System.out.println("Client Message= "+str);

server.close();

catch(Exception e)

System.out.println(e);

}
Client.java

import java.io.*;

import java.net.*;

public class Client {

public static void main(String[] args) {

try

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

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

dout.writeUTF("Hey this is from client");

dout.flush();

dout.close();

s.close();

catch(Exception e)

System.out.println(e);

2. using DatagramSocket

import java.io.*;

import java.net.*;
class UDPServer

public static DatagramSocket serversocket;

public static DatagramPacket dp;

public static BufferedReader dis;

public static InetAddress ia;

public static byte buf[] = new byte[1024];

public static int cport = 789,sport=790;

public static void main(String[] a) throws IOException

serversocket = new DatagramSocket(sport);

dp = new DatagramPacket(buf,buf.length);

dis = new BufferedReader

(new InputStreamReader(System.in));

ia = InetAddress.getLocalHost();

System.out.println("Server is Running...");

while(true)

serversocket.receive(dp);

String str = new String(dp.getData(), 0,dp.getLength());

if(str.equals("Chaw"))

System.out.println("Terminated..");

break;

}
System.out.println("Client Message: " + str);

String str1 = new String(dis.readLine());

buf = str1.getBytes();

serversocket.send(new DatagramPacket(buf,str1.length(), ia, cport));

import java.io.*;

import java.net.*;

class UDPClient

public static DatagramSocket clientsocket;

public static DatagramPacket dp;

public static BufferedReader dis;

public static InetAddress ia;

public static byte buf[] = new byte[1024];

public static int cport = 789, sport = 790;

public static void main(String[] a) throws IOException

clientsocket = new DatagramSocket(cport);

dp = new DatagramPacket(buf, buf.length);

dis = new BufferedReader(new

InputStreamReader(System.in));

ia = InetAddress.getLocalHost();
System.out.println("Client is saying Chaw");

while(true)

String str = new String(dis.readLine());

buf = str.getBytes();

if(str.equals("Chaw"))

System.out.println("Terminated...");

clientsocket.send(newDatagramPacket(buf,str.length(), ia,sport));

break;

clientsocket.send(new DatagramPacket(buf,str.length(), ia, sport));

clientsocket.receive(dp);

String str2 = new String(dp.getData() 0,dp.getLength());

System.out.println("Server Message: " + str2);

Melese Alemante9:35 AM

Great!.

You might also like