Question: Write a Client-Server application using TCP Socket where client can send message and server respond with the reverse of them.
package MyClient;
import java.io.*;
import java.net.*;
public class MyClient
{
public static void main(String[] args) throws IOException
{
try
{
Socket s = new Socket("localhost",6666);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("GUJARAT TECHNOLOGICAL UNIVERSITY");
dout.flush();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
MyServer.java:
package MyServer;
import java.io.*;
import java.net.*;
public class MyServer
{
public static void main(String[] args) throws IOException
{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String) dis.readUTF();
StringBuffer buffer = new StringBuffer(str);
buffer.reverse();
System.out.println(buffer);
}
}
Output: