how to crate clint-server socket?

Answer Posted / dinesh tiwari

//SERVER code///////////////////////////////
import java.io.*;
import java.net.*;
public class Provider{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Provider(){}
void run()
{
try{
//1. creating a server socket
providerSocket = new ServerSocket
(2004, 10);
//2. Wait for connection
System.out.println("Waiting for
connection");
connection = providerSocket.accept
();
System.out.println("Connection
received from " + connection.getInetAddress().getHostName
());
//3. get Input and Output streams
out = new ObjectOutputStream
(connection.getOutputStream());
out.flush();
in = new ObjectInputStream
(connection.getInputStream());
sendMessage("Connection
successful");
//4. The two parts communicate via
the input and output streams
do{
try{
message = (String)
in.readObject();
System.out.println
("client>" + message);
if (message.equals
("bye"))
sendMessage
("bye");
}
catch
(ClassNotFoundException classnot){
System.err.println
("Data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace
();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Provider server = new Provider();
while(true){
server.run();
}
}
}

........................................................
CLIENT CODE
........................................................
import java.io.*;
import java.net.*;
public class Requester{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Requester(){}
void run()
{
try{
//1. creating a socket to connect
to the server
requestSocket = new Socket
("localhost", 2004);
System.out.println("Connected to
localhost in port 2004");
//2. get Input and Output streams
out = new ObjectOutputStream
(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream
(requestSocket.getInputStream());
//3: Communicating with the server
do{
try{
message = (String)
in.readObject();
System.out.println
("server>" + message);
sendMessage("Hi my
server");
message = "bye";
sendMessage
(message);
}
catch
(ClassNotFoundException classNot){
System.err.println
("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying
to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace
();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("client>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Requester client = new Requester();
client.run();
}
}

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is difference between calling start() and run() method of thread?

555


What are keyboard events?

602


What is the significance of listiterator?

562


What is the difference between jfc & wfc?

587


What are white spaces in java?

520






What is difference between string and new string?

528


What is meant by JVM? Is JVM platform independent or not?

569


List any five features of java?

562


what is method reference in java 8?

550


What are the basic control structures?

492


What is the name of the java compiler?

512


What are singleton services?

486


Is it possible to override the main method?

525


What is :: operator in java 8?

552


What is variable and constant explain with example?

525