Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.Scanner;
- public class TCPClient extends Thread{
- private String serverName;
- private int serverPort;
- public TCPClient(String serverName, int serverPort) {
- this.serverName = serverName;
- this.serverPort = serverPort;
- }
- @Override
- public void run() {
- Socket socket = null;
- Scanner scanner = null;
- PrintWriter writer = null;
- BufferedReader reader = null;
- try {
- socket = new Socket(serverName, serverPort);
- writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
- reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- scanner = new Scanner(System.in);
- while (true) {
- String receivedMessage = reader.readLine();
- System.out.println(receivedMessage);
- String line = scanner.nextLine();
- writer.println(line);
- writer.flush();
- }
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- TCPClient client = new TCPClient("localhost",8888);
- client.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement