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();
}
}
}