-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPServer.java
More file actions
executable file
·49 lines (34 loc) · 1.3 KB
/
Copy pathTCPServer.java
File metadata and controls
executable file
·49 lines (34 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
*
* tcpServer from Kurose and Ross
*
* Usage: java TCPServer [server port]
*/
import java.io.*;
import java.net.*;
public class TCPServer {
public static void main(String[] args)throws Exception {
// see if we do not use default server port
int serverPort = 6789;
/* change above port number this if required */
if (args.length >= 1)
serverPort = Integer.parseInt(args[0]);
// create server socket
ServerSocket welcomeSocket = new ServerSocket(serverPort);
while (true){
// accept connection from connection queue
Socket connectionSocket = welcomeSocket.accept();
System.out.println("connection from " + connectionSocket);
// create read stream to get input
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
String clientSentence;
clientSentence = inFromClient.readLine();
// process input
String capitalizedSentence;
capitalizedSentence = clientSentence.toUpperCase() + '\n';
// send reply
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
outToClient.writeBytes(capitalizedSentence);
} // end of while (true)
} // end of main()
} // end of class TCPServer