四十三、多线程多客户端
程序员文章站
2024-03-22 23:23:16
...
package run;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import oop.User;
public class Cli {
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
Socket soc = new Socket("192.168.1.9",8800);
OutputStream os = soc.getOutputStream();
InputStream is = soc.getInputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
User user = new User();
user.setLoginName("Tom");
user.setPwd("123456");
oos.writeObject(user);
soc.shutdownOutput();
os.close();
is.close();
}
}
package run;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import oop.User;
public class LoginThread extends Thread{
Socket socket=null;
public LoginThread(Socket socket) {
this.socket=socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
InputStream is;
try {
is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
ObjectInputStream ons = new ObjectInputStream (is);
User user = (User)ons.readObject();
if (!(user==null)) {
System.out.println("我是服务器,客户端输出信息为:"+user.getLoginName()+","+user.getPwd());
}
is.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
上一篇: Shiro框架学习
下一篇: iOS-新手引导页+标签控制器