TCP/IP client sockets

Posted By on April 29, 2016


Download PDF
Network Basics and Socket overview
Java URL

Client/server socket interaction: TCP

Typically a client opens a TCP/IP connection to a server. The client then starts to communicate with the server. When the client is finished it closes the connection again.

tcpsocket

TCPClient.java

import java.io.*;
import java.net.*;

class TCPClient {
public static void main(String argv[]) throws Exception{
String sentence;
String modifiedSentence;

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

Socket clientSocket = new Socket(“localhost”, 6789);

DataOutputStream outToServer = new DataOutputStream (clientSocket.getOutputStream());

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

sentence = inFromUser.readLine();

outToServer.writeBytes(sentence+‘\n’);

modifiedSentence = inFromServer.readLine();

System.out.println(“FROM SERVER: ” + modifiedSentence);

clientSocket.close();
}
}

 

TCPServer.java

import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;

ServerSocket welcomeSocket = new ServerSocket(6789);

while(true) {

Socket connectionSocket = welcomeSocket.accept();

BufferedReader inFromClient = new BufferedReader(new

InputStreamReader(connectionSocket.getInputStream()));

DataOutputStream  outToClient =   new DataOutputStream (connectionSocket.getOutputStream());

clientSentence = inFromClient.readLine();

capitalizedSentence = clientSentence.toUpperCase() + ‘\n’;

outToClient.writeBytes(capitalizedSentence);
}
}
}

Network Basics and Socket overview
Java URL

Download PDF