java Socket

Socket

Socket即套接字,用于计算机通信,以一定的格式和方式让其可以向别的计算机发送信息或接收信息。
其实真正完成计算机通信的是Socket背后复杂的TCP/IP协议族。TCP/IP协议一般认为分为四层:应用层,运输层(TCP,UDP),网路层(IP),网络接口层(物理层和数据链路层)
在这里插入图片描述
而Socket是处于应用层何运输层之间的抽象层,对于用户来说,只需要将信息交给Socket,Socket就能组织处理信息使其符合协议。

Socket可以视为计算机通信的两个端点,可以对其进行open,read/write,close操作,通信的一方将信息写入Socket,然后Socket通过各种方式介质将信息运输到另一端Socket中。

Socket的实现流程

在这里插入图片描述
建立连接过程
客户端--------发送请求--------->服务器监听----------->建立连接

然后可以通过while()方式不停的发送请求和响应,为了能与多台客户端交互,可以采用多线程方式。

共有类 User(在传递对象流时,对象类要进行序列化,而且服务端和客户端的对象类包名也要一样)


import java.io.Serializable;

public class User implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -902146686676414903L;
	
	
	private int id;
	private String name;
	private String paw;
	
	public User(int id, String name, String paw) {
		super();
		this.id = id;
		this.name = name;
		this.paw = paw;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPaw() {
		return paw;
	}

	public void setPaw(String paw) {
		this.paw = paw;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", paw=" + paw + "]";
	}
	

}

Client

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClientMain {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 try {
				Socket socket = new Socket("localhost", 9001);
				
			   
			   
				ClientThread ct =new ClientThread(socket);
				ct.start();
				
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

	}

}

Client Thread

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import common.User;

public class ClientThread extends Thread{
	private Socket s;
	private ObjectOutputStream oos;
    private ObjectInputStream ois;
	public ClientThread(Socket s) throws IOException {
		super();
		this.s = s;
		
		this.ois = new ObjectInputStream( s.getInputStream());
		this.oos = new ObjectOutputStream(s.getOutputStream());
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		try {
			oos.writeObject(new User(1001,"zjj","ppp"));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Object obj = null;
		try {
			while((obj = ois.readObject())!=null)
			{
				User user = (User) obj;
				System.out.println(user);
			}
		} catch (ClassNotFoundException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

Server

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerMain {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
	
			ServerSocket serverSocket = new ServerSocket(9001);
			while (true) {
				Socket socket = serverSocket.accept();		
				ServerThread st = new ServerThread(socket);
				st.start();
			
		}

	}

}

Server Thread

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import common.User;

public class ServerThread extends Thread{
	private Socket s;
	private ObjectOutputStream oos;
	private ObjectInputStream ois;

	public ServerThread(Socket s) throws IOException {
		super();
		this.s = s;
		oos = new ObjectOutputStream(s.getOutputStream());
		ois = new ObjectInputStream( s.getInputStream());
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		Object obj = null;
		try {
			while((obj = ois.readObject())!= null)
			{
				try {
					User user = (User)obj;
					System.out.println(user);
					
					if(user.getId()==1001)
					{
						oos.writeObject(new User(1002,"zjj2","paw"));
						
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		} catch (ClassNotFoundException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

简易聊天室实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值