-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpserver.java
More file actions
26 lines (24 loc) · 1.03 KB
/
tcpserver.java
File metadata and controls
26 lines (24 loc) · 1.03 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
// Program: TCP Server used to interact with local TCP client
import java.io.*;
import java.net.*;
public class tcpserver {
public static void main(String[] args){
try {
ServerSocket server = new ServerSocket(5000, 10); // Create server, 10 clients max
Socket connection = server.accept();
// Create input/output streams for data transmission
ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
// Creation connection message, write to client
String message = "Connection successful";
out.writeObject(message);
// Read in data sent from Client
message = (String) in.readObject();
System.out.println("Data received from Client:" + message);
connection.close();
}// end try
catch (Exception ex){
System.err.println(ex);
}// end catch
}// main
}// end class