Files
operating-system-second-ass…/Problem 3.18/SocketServer.java
2019-03-17 23:28:35 +08:00

38 lines
1.2 KiB
Java

import java.net.*;
import java.io.*;
public class SocketServer {
public static void main(String[] args) {
ServerSocket sock = null;
Socket client = null;
try {
sock = new ServerSocket(6017);
client = sock.accept();
DataInputStream inputStream = new DataInputStream(client.getInputStream());
DataOutputStream outputStream = new DataOutputStream(client.getOutputStream());
while (true) {
try {
String message = inputStream.readUTF();
System.out.println("Client: " + message);
outputStream.writeUTF(message);
} catch (IOException e) {
if (e.getMessage().equals("null")) {
break;
}
}
}
inputStream.close();
outputStream.close();
client.close();
sock.close();
}
catch (IOException ioe) {
try {
sock.close();
client.close();
} catch (IOException e) {}
System.err.println(ioe);
}
}
}