TCPClient
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 client = new Socket("localhost", 20000);
DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
client.close();
}
}
TCPServer
import java.io.*;
import java.net.*;
class TCPServer{
public static void main(String argv[]) throws Exception{
String clientSentence;
String capitalizedSentence;
InetAddress address;
ServerSocket welcomeSocket = new ServerSocket(20000);
while(true){
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
address = InetAddress.getByName(clientSentence);
capitalizedSentence = address + " Thank You " +'\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
Tidak ada komentar:
Posting Komentar