0% found this document useful (0 votes)
431 views2 pages

AJP No.16

The document describes a Java program that implements a chat server using ServerSocket and Socket classes. The server side program creates a ServerSocket that listens on port 1234 for connection requests. When a client connects, it receives and sends messages to the client. The client side program connects to the server on localhost port 1234, sends a message, receives a response, and prints both to standard output. The programs demonstrate basic client-server message passing over sockets in Java.

Uploaded by

Saraswati Shelke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
431 views2 pages

AJP No.16

The document describes a Java program that implements a chat server using ServerSocket and Socket classes. The server side program creates a ServerSocket that listens on port 1234 for connection requests. When a client connects, it receives and sends messages to the client. The client side program connects to the server on localhost port 1234, sends a message, receives a response, and prints both to standard output. The programs demonstrate basic client-server message passing over sockets in Java.

Uploaded by

Saraswati Shelke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical No.

: 16
Title: Write a Program to implement chat server using ServerSocket &Socket class.

• Server Side Program


package serversocketex;
import java.net.*;
import java.io.*;
public class ServerSocketEx
{
public static void main(String[] args) throws IOException
{
ServerSocket SS=new ServerSocket(1234);
Socket S=SS.accept();
OutputStream o=S.getOutputStream();
DataOutputStream dos=new DataOutputStream(o);
InputStream i=S.getInputStream();
DataInputStream dis=new DataInputStream(i);
String str=dis.readUTF();
System.out.println(str);
dos.writeUTF("Good Morning! Client");
System.out.println("Respose send to the Client Successfully...");
dos.close();
SS.close();
}
}
Output:
Good Morning! Server...
Respose send to the Client Successfully...

• Client Side Program:


package socketex;
import java.net.*;
import java.io.*;
public class SocketEx
{
public static void main(String[] args) throws IOException
{
Socket S=new Socket("localhost",1234);
InputStream i=S.getInputStream();

DataInputStream dis=new DataInputStream(i);


OutputStream o=S.getOutputStream();
DataOutputStream dos=new DataOutputStream(o);
dos.writeUTF("Good Morning! Server...");
String str1=dis.readUTF();
System.out.println(str1);
System.out.println("Respose Recieved From the Server Successfully...");
dis.close();
S.close();
}

}
Output:
Good Morning! Client
Respose Recieved From the Server Successfully...

You might also like