SHOW:
|
|
- or go back to the newest paste.
| 1 | import java.io.*; | |
| 2 | import java.net.*; | |
| 3 | - | |
| 3 | + | public class TCPServer |
| 4 | - | class TCPServer |
| 4 | + | |
| 5 | public static void main(String argv[]) throws Exception | |
| 6 | - | public static void main(String argv[]) throws Exception |
| 6 | + | {
|
| 7 | - | {
|
| 7 | + | ServerSocket welcomeSocket = new ServerSocket(3001); |
| 8 | - | String clientSentence; |
| 8 | + | while(true) |
| 9 | - | String capitalizedSentence; |
| 9 | + | {
|
| 10 | - | ServerSocket welcomeSocket = new ServerSocket(3001); |
| 10 | + | |
| 11 | - | |
| 11 | + | new Thread(new TCPServer(). new Client(connectionSocket)).start(); |
| 12 | - | while(true) |
| 12 | + | } |
| 13 | - | {
|
| 13 | + | } |
| 14 | public class Client implements Runnable{
| |
| 15 | - | BufferedReader inFromClient = |
| 15 | + | Socket client; |
| 16 | - | new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); |
| 16 | + | String clientSentence; |
| 17 | - | DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); |
| 17 | + | String capitalizedSentence; |
| 18 | - | clientSentence = inFromClient.readLine(); |
| 18 | + | public Client(Socket s){
|
| 19 | - | System.out.println("Received: " + clientSentence);
|
| 19 | + | client = s; |
| 20 | - | capitalizedSentence = clientSentence.toUpperCase() + '\n'; |
| 20 | + | } |
| 21 | - | outToClient.writeBytes(capitalizedSentence); |
| 21 | + | @Override |
| 22 | - | } |
| 22 | + | public void run(){
|
| 23 | - | } |
| 23 | + | try{
|
| 24 | BufferedReader inFromClient = | |
| 25 | new BufferedReader(new InputStreamReader(client.getInputStream())); | |
| 26 | DataOutputStream outToClient = new DataOutputStream(client.getOutputStream()); | |
| 27 | ||
| 28 | while((clientSentence = inFromClient.readLine())!=null){
| |
| 29 | System.out.println("Received: " + clientSentence);
| |
| 30 | capitalizedSentence = clientSentence.toUpperCase() + '\n'; | |
| 31 | outToClient.writeBytes(capitalizedSentence); | |
| 32 | } | |
| 33 | }catch(Exception e){e.printStackTrace();}
| |
| 34 | } | |
| 35 | } | |
| 36 | } |