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

Practical-5: Aim: Create A Client - Server Application Using Socket Programming. Server File

This document describes a client-server application created using socket programming in Java. The server file creates a ServerSocket that listens for connections, and upon receiving a connection it sends and receives messages from the client. The client file creates a Socket to connect to the server, and sends and receives messages from the server by writing and reading to output and input streams. The client and server can successfully exchange messages by sending strings between each other's input and output streams.

Uploaded by

Jalpa Pandya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Practical-5: Aim: Create A Client - Server Application Using Socket Programming. Server File

This document describes a client-server application created using socket programming in Java. The server file creates a ServerSocket that listens for connections, and upon receiving a connection it sends and receives messages from the client. The client file creates a Socket to connect to the server, and sends and receives messages from the server by writing and reading to output and input streams. The client and server can successfully exchange messages by sending strings between each other's input and output streams.

Uploaded by

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

AJT(170703) 120123116020

Practical-5
Aim: Create a Client Server application using Socket programming.
Server file
import ava.io.!"
import ava.net.!"
pu#lic class $rovi%er&
ServerSocket provi%erSocket"
Socket connection ' null"
(#ect(utputStream out"
(#ect)nputStream in"
String message"
$rovi%er()&*
voi% run()
&
Tr+&
provi%erSocket ' ne, ServerSocket(200-. 10)"
S+stem.out.println(/0aiting 1or connection/)"
connection ' provi%erSocket.accept()"
S+stem.out.println(/Connection receive% 1rom / 2
connection.get)netA%%ress().get3ost4ame())"
out ' ne, (#ect(utputStream(connection.get(utputStream())"
out.1lus5()"
in ' ne, (#ect)nputStream(connection.get)nputStream())"
sen%6essage(/Connection success1ul/)"
%o&
tr+&
message ' (String)in.rea%(#ect()"
S+stem.out.println(/client7/ 2 message)"
i1 (message.e8uals(/#+e/))
sen%6essage(/#+e/)"
*
catc5(Class4ot9oun%:;ception classnot)&
S+stem.err.println(/<ata receive% in unkno,n 1ormat/)"
*
*,5ile(=message.e8uals(/#+e/))"
*
catc5()(:;ception io:;ception)&
1>
AJT(170703) 120123116020
io:;ception.printStackTrace()"
*
1inall+&
tr+&
in.close()"
out.close()"
provi%erSocket.close()"
*
catc5()(:;ception io:;ception)&
io:;ception.printStackTrace()"
*
*
*
voi% sen%6essage(String msg)
&
tr+&
out.,rite(#ect(msg)"
out.1lus5()"
S+stem.out.println(/server7/ 2 msg)"
*
catc5()(:;ception io:;ception)&
io:;ception.printStackTrace()"
*
*
pu#lic static voi% main(String args?@)
&
$rovi%er server ' ne, $rovi%er()"
,5ile(true)&
server.run()"
*
*
*
Client file
import ava.io.!"
import ava.net.!"
pu#lic class Ae8uester&
Socket re8uestSocket"
(#ect(utputStream out"
(#ect)nputStream in"
20
AJT(170703) 120123116020
String message"
Ae8uester()&*
voi% run()
&
Tr+&
re8uestSocket ' ne, Socket(/local5ost/. 200-)"
S+stem.out.println(/Connecte% to local5ost in port 200-/)"
out ' ne, (#ect(utputStream(re8uestSocket.get(utputStream())"
out.1lus5()"
in ' ne, (#ect)nputStream(re8uestSocket.get)nputStream()"
%o&
tr+&
message ' (String)in.rea%(#ect()"
S+stem.out.println(/server7/ 2 message)"
sen%6essage(/3i m+ server/)"
message ' /#+e/"
sen%6essage(message)"
*
catc5(Class4ot9oun%:;ception class4ot)&
S+stem.err.println(/%ata receive% in unkno,n 1ormat/)"
*
*,5ile(=message.e8uals(/#+e/))"
*
catc5(Bnkno,n3ost:;ception unkno,n3ost)&
S+stem.err.println(/Cou are tr+ing to connect to an unkno,n 5ost=/)"
*
catc5()(:;ception io:;ception)&
io:;ception.printStackTrace()"
*
1inall+&
tr+&
in.close()"
out.close()"
re8uestSocket.close()"
*
catc5()(:;ception io:;ception)&
io:;ception.printStackTrace()"
*
*
*
voi% sen%6essage(String msg)
21
AJT(170703) 120123116020
&
tr+&
out.,rite(#ect(msg)"
out.1lus5()"
S+stem.out.println(/client7/ 2 msg)"
*
catc5()(:;ception io:;ception)&
io:;ception.printStackTrace()"
*
*
pu#lic static voi% main(String args?@)
&
Ae8uester client ' ne, Ae8uester()"
client.run()"
*
*
OUTPUT:
Server
22
AJT(170703) 120123116020
Client
23

You might also like