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());
//
// }
}
}
然后是服务器端代码,注释部分也是不用线程的方法实现连接。
其中包括,一个客户端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编程,实现简单的客户端服务器端发送数据
- 用socket实现客户端与服务器端的通信,要求在客户发送数据后能够回显相同的数据。
- android端和pc端使用usb进行socket通信,其中android是服务器端,pc是客户端。如何实现安卓端输入的数据通过按钮发送到pc端?
- ios客户端向服务器端发送数据以及接收数据要如何实现?
- Android中使用Socket来实现服务器端和客户端,进行数据通信(使用原生Socket)
- Socket编程如何实现服务器端接收客户端数据
- 简单socket服务(二)实现多客户端向服务器发送数据
- C# socket 实现客户端连续发送数据
- 简单socket服务(三)实现多客户端向服务器发送数据并保证服务器接收到数据
- Android简单实现Socket通信,客户端连接服务器后,服务器向客户端发送文字数据
- VC写的socket程序实现一个简单Echo服务器端和客户端程序
- 利用线程池实现多客户端和单服务器端Socket通讯(二):异步编程模型实现
- Socket实现的SMTP邮件发送客户端。
- 关于用Socket实现,客户端和服务器端的通信
- 第一个java网络程序,有客户端和服务器端,用socket实现
- 利用线程池实现多客户端和单服务器端Socket通讯(一):同步方式
- Asp.net Socket 客户端(发送和接收数据
- 一个简单的socket客户端发送消息到服务器端程序
- WSAAsyncSelect模型 实现socket客户端接收数据的例子
- C# 视频监控系列(5):客户端——给服务器端发送字符串和录像(数据捕获)