userthread.java
来自「java网络高级编程的配套源码,java网络高级编程为清华出版社出版.」· Java 代码 · 共 75 行
JAVA
75 行
/*源程序清单8-15*/
import java.io.*;
import java.net.*;
public class UserThread extends Thread
{
protected DataInputStream inStream;
protected DataOutputStream outStream;
protected Socket socket;
public UserThread(Socket socket)
{
try
{
this.socket=socket;
inStream=new DataInputStream(socket.getInputStream());
outStream=new DataOutputStream(socket.getOutputStream());
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public void run()
{
String str=null;
try
{
while(true)
{
str=inStream.readUTF();
dataProcess(str);
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
finally
{
try
{
socket.close();
}
catch(IOException e1)
{
System.out.println(e1.toString());
}
}
}
public void dataProcess(String msg)
{
try
{
System.out.println("服务器接收到数据"+msg);
String newMsg;
newMsg=" 服务器接收到数据:"+msg;
sendString(newMsg);
}
catch(IOException e)
{
System.out.println(e.toString());
}
}
public void sendString(String msg) throws IOException
{
outStream.writeUTF(msg);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?