TCP/IP Client Server program example
Here is a simple example of client-server operation using two simple Java programs. There are
two programs, Server.java and Client.java.
The server returns the input message with all lowercase letters converted to uppercase.
These programs are missing some instructions.
The server program:
// import packages
classTCPServer {
publicstaticvoidmain(String argv[]) throwsException {
String clientSentence;
String capitalizedSentence;
//create Socket
..
while (true) {
//Connection
BufferedReaderinFromClient =
newBufferedReader(newInputStreamReader(connectionSocket.getInputStream()));
DataOutputStreamoutToClient = newDataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine(); //read what the client sent
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + 'n';
outToClient.(capitalizedSentence); //writing to client
}
}
}
The client program
// Import packages
classTCPClient {
publicstaticvoidmain(String argv[]) throws Exception {
String sentence;
String modifiedSentence;
BufferedReaderinFromUser = newBufferedReader(newInputStreamReader(System.in));
//Connection
.
//Writing in the socket
.
BufferedReaderinFromServer =
newBufferedReader(newInputStreamReader(clientSocket.getInputStream()));
System.out.print("Enter a sentence: ");
sentence = inFromUser.readLine(); //reads a sentancefrom the user
.//send to server
modifiedSentence = inFromServer.readLine(); //recievefrom server
System.out.println("FROM SERVER: " + modifiedSentence);
//Close the socket
.
}
}

TCPIP Client Server program exampleHere is a simple example of .pdf

  • 1.
    TCP/IP Client Serverprogram example Here is a simple example of client-server operation using two simple Java programs. There are two programs, Server.java and Client.java. The server returns the input message with all lowercase letters converted to uppercase. These programs are missing some instructions. The server program: // import packages classTCPServer { publicstaticvoidmain(String argv[]) throwsException { String clientSentence; String capitalizedSentence; //create Socket .. while (true) { //Connection BufferedReaderinFromClient = newBufferedReader(newInputStreamReader(connectionSocket.getInputStream())); DataOutputStreamoutToClient = newDataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); //read what the client sent System.out.println("Received: " + clientSentence); capitalizedSentence = clientSentence.toUpperCase() + 'n'; outToClient.(capitalizedSentence); //writing to client
  • 2.
    } } } The client program //Import packages classTCPClient { publicstaticvoidmain(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReaderinFromUser = newBufferedReader(newInputStreamReader(System.in)); //Connection . //Writing in the socket . BufferedReaderinFromServer = newBufferedReader(newInputStreamReader(clientSocket.getInputStream())); System.out.print("Enter a sentence: "); sentence = inFromUser.readLine(); //reads a sentancefrom the user .//send to server modifiedSentence = inFromServer.readLine(); //recievefrom server System.out.println("FROM SERVER: " + modifiedSentence); //Close the socket . }
  • 3.