CN Lab-3

//TCP Server using Socket programming.

import java.net.*;

import java.io.*;

/**

* Write a description of class TCPServer here.

*

* @author (your name)

* @version (a version number or a date)

*/

public class TCPServer

{

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

{

// create a serversocket that listens via port number 6969

ServerSocket serversocketobj=new ServerSocket(6969);

//wait till a client connects to the server

//and create a separate socket object for further exchange of messages

//with client

System.out.println("waiting for connection from client");

Socket clientsocket= serversocketobj.accept();

//Attach an input stream to the socket to facilitate receiving from client

//buffer the characters that are received via the inputStreamReader

//so that we can read a line of input info from client at one stroke

BufferedReader infromClient= new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));

String inputstring= infromClient.readLine();

System.out.println("From Client:"+ inputstring);

//Attach an output stream to echo the input received from the client

//back to it (after converting all into uppercase)

PrintWriter OutToclient=new PrintWriter(clientsocket.getOutputStream());

String output=inputstring.toUpperCase();

OutToclient.println("From Server:"+output);

OutToclient.flush();

}

// instance variables - replace the example below with your own

}