Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package socketsMoreClients;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.InputStreamReader;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class TCPServer {
- public static void main(String argv[]) throws Exception
- {
- System.out.println("starting main");
- String sentence;
- String userText;
- boolean go_on = true;
- ServerSocket welcomeSocket = new ServerSocket(6780);
- System.out.println("we have a socket");
- BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("waiting for a connection");
- Socket connectionSocket = welcomeSocket.accept();
- BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
- DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
- sentence = inFromClient.readLine();
- System.out.println("FROM CLIENT: " + sentence);
- while(go_on)
- {
- System.out.print("Please type your text: ");
- userText = inFromUser.readLine();
- outToClient.writeBytes(userText + '\n');
- sentence = inFromClient.readLine();
- System.out.println("FROM CLIENT: " + sentence);
- if (sentence.equals("quit")) { go_on = false; }
- }
- System.out.println("quitting the loop and closing the socket");
- connectionSocket.close();
- welcomeSocket.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment