您的位置:首页 > 其它

通信之多客户端传输对象实现

2016-07-12 11:20 295 查看
根据慕课网Java Socket前面的章节,改装实现的5-1节Socket总结中的Socket通信,实现了多客户端传输对象。
1.客户端Client.java的实现
public class Client {

public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//1.创建客户端Socket,指定服务器地址和端口
Socket socket=new Socket("localhost", 8888);
//2.获取输出流,向服务器端发送信息
OutputStream os=socket.getOutputStream();//字节输出流
ObjectOutputStream oos = new ObjectOutputStream(os);
User user = new User("admin","123"); //封装为对象
oos.writeObject(user);//序列化
socket.shutdownOutput();//关闭输出流
//3.获取输入流,并读取服务器端的响应信息
InputStream is=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String info=null;
while((info=br.readLine())!=null){
System.out.println("我是客户端,服务器说:"+info);
}
//4.关闭资源
br.close();
is.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}
2.服务端 Server.java的实现
public class Server {

public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//1.创建一个服务器端Socket,即ServerSocket,指定绑定的端口,并监听此端口
ServerSocket serverSocket=new ServerSocket(8888);
Socket socket=null;
//记录客户端的数量
int count=0;
System.out.println("***服务器即将启动,等待客户端的连接***");
//循环监听等待客户端的连接
while(true){
//调用accept()方法开始监听,等待客户端的连接
socket=serverSocket.accept();
//创建一个新的线程
ServerThread serverThread=new ServerThread(socket);
//启动线程
serverThread.start();
count++;//统计客户端的数量
System.out.println("客户端的数量:"+count);
InetAddress address=socket.getInetAddress();
System.out.println("当前客户端的IP:"+address.getHostAddress());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


3.多线程端 ServerThread.java的实现
public class ServerThread extends Thread {
//和本线程相关的Socket
Socket socket = null;

public ServerThread(Socket socket) {
this.socket = socket;
}

//线程执行的操作,响应客户端的请求
public void run(){
InputStream is=null;
InputStreamReader isr=null;
BufferedReader br=null;
OutputStream os=null;
PrintWriter pw=null;
ObjectInputStream ois=null;
try {
//获取输入流,并读取客户端信息
is = socket.getInputStream();
/*
isr = new InputStreamReader(is);
br = new BufferedReader(isr);*/
ois=new ObjectInputStream(is);
User us=(User)ois.readObject();
System.out.println("我是服务器,客户端说:用户名:"+us.getName()+" id:"+us.getId());
/*
String info=null;
while((info=br.readLine())!=null){//循环读取客户端的信息
System.out.println("我是服务器,客户端说:"+info);
}*/
socket.shutdownInput();//关闭输入流
//获取输出流,响应客户端的请求
os = socket.getOutputStream();
pw = new PrintWriter(os);
pw.write("欢迎您!");
pw.flush();//调用flush()方法将缓冲输出
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭资源
try {
if(pw!=null)
pw.close();
if(os!=null)
os.close();
if(br!=null)
br.close();
if(isr!=null)
isr.close();
if(is!=null)
is.close();
if(socket!=null)
socket.close();
if(ois!=null)
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


4.对象类User.java的实现
public class User implements Serializable{
String id;
String name;
User(){

}
User(String name,String id){
this.id=id;
this.name=name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: