您的位置:首页 > 其它

socket用线程实现客户端和服务器端连续发送数据

2016-10-31 16:34 453 查看
总共涉及到四个类。

其中包括,一个客户端client,一个服务端server,

然后服务器端和客户端都有发送和接收的功能,所以还有一个发送类sendThread实现runnable接口,还有个接收类receiveThread实现runnable接口。

在客户端client和服务端分别实现发送和接收的工能就好了。

这是客户端代码,注释部分为不用线程的方法实现连接。
package qq;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
//总结,如果用线程的话1.可以让发送和接收这两个方法异步执行,互不干扰。
//2.可以发送和接收这两个方法一直处于活跃状态。
//如果不用线程的话你会发现发送和接收这两个方法一定要按顺序来。不能连续发送两次数据
public class QQClient {
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException {

Socket s=new Socket("localhost",1994);
SendThread st=new SendThread(s);
new Thread(st).start();
new Thread(new ReceiveThread(s)).start();
// while(true){
// ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
// oos.writeObject(new Student(new Scanner(System.in).nextInt(),"2","1",25,"c1",100));
// oos.flush();
//
// ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(s.getInputStream()));
// Student stu=(Student)ois.readObject();
// System.out.println(stu.getBianhao());
//
// }
}
}
然后是服务器端代码,注释部分也是不用线程的方法实现连接。
package qq;

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

public class QQServer {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ServerSocket ss=new ServerSocket(1994);
Socket s=ss.accept();
SendThread st=new SendThread(s);
new Thread(st).start();
new Thread(new ReceiveThread(s)).start();
//		while(true){
//			ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
//			oos.writeObject(new Student(new Scanner(System.in).nextInt(),"2","1",25,"c1",100));
//			oos.flush();
//		ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(s.getInputStream()));
//		Student stu=(Student)ois.readObject();
//		System.out.println(stu.getBianhao());
//
//
//		}
}
}
然后是实现runnable接口发送功能的类
package qq;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

import aboutsocket.Student;

public class SendThread implements Runnable{
private Socket s;
public SendThread(Socket s){
this.s=s;
}
@Override
public void run() {
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
while(true){
oos.writeObject(new Student(new Scanner(System.in).nextInt(),"2","1",25,"c1",100));
oos.flush();
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

}

}
接收功能的
package qq;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

import aboutsocket.Student;

public class SendThread implements Runnable{
private Socket s;
public SendThread(Socket s){
this.s=s;
}
@Override
public void run() {
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
while(true){
oos.writeObject(new Student(new Scanner(System.in).nextInt(),"2","1",25,"c1",100));
oos.flush();
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

}

}
刚学习socket的时候可能会想为什么一定要用线程来呢,即为什么不直接使用上面注释掉的代码呢?因为你运行完后会发现接收和发送功能如果在一起的话,会按秩序执行,即只能客户端发一句话,然后服务器端接收完后服务器端发送一次,然后客户端接收客户端再发送一次,如此循环。那如果我想像QQ一样能一个人连续说几句话,那我们就要将发送和接收两个方法分开来,这就有了两个线程将这两个方法封装起来,并且线程的话本身功能就有异步的功能,即互不影响。这就可以实现客户端连续发送消息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  socket 线程
相关文章推荐