📄 serthread.java
字号:
import java.io.*;
import java.net.*;
public class serthread extends Thread
{ Socket socket;
public serthread(Socket s)
{ socket=s;
start();
}
public void run()
{
try { System.out.println("Accept Connection" +socket+"\n");
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),true);
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out.println("I want your time!");
System.out.println("Server: Now I want your time!");
String str=in.readLine();
System.out.println("Server: "+str);
out.println("Now the time is 21:00 !");
}
catch(IOException e)
{ System.err.println("IO Exception");
}
finally{
System.out.println("Closing ...");
try
{socket.close();}
catch(IOException e)
{System.err.println("Socket not closed");}
}
}
// static final int PORT=4000;
public static void main(String[] args)throws IOException
{
ServerSocket s1=new ServerSocket(4000);
ServerSocket s2=new ServerSocket(4001);
System.out.print("Server Start. ");
try
{ Socket socket1=s1.accept();
Socket socket2=s2.accept();
try
{ new serthread(socket1);
new serthread(socket2);
}
catch(Exception e)
{socket1.close();
socket2.close();
}
}
finally
{ s1.close();
s2.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -