public class Server {
public static void main(String[] args) {
try {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("serverFile.txt");
ServerSocket ss = new ServerSocket(8099);
System.out.println("服务器已启动,等到客户端的连接...");
Socket socket = ss.accept(); // -->服务器收到客户端的数据后,创建与此客户端对话的Socket
DataInputStream in = new DataInputStream(socket.getInputStream());
// -->用于接收客户端 发来的数据的输入流
System.out.println("服务器接受到客户端的连接请求:" + in.readUTF());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String data = null;
StringBuffer sb = new StringBuffer();
while((data = br.readLine())!=null)
{
sb.append(data);
}
out.writeUTF(sb.toString());//服务器响应给客户端
socket.close();
ss.close();
} catch (Exception e) {
e.printStackTrace();
}
}