java socket多客户_Java socket ,多客户端同时与服务端不停的交换数据

本文介绍了如何使用Java Socket实现多客户端同时与服务端进行不间断的数据交换。服务端通过循环监听和创建新线程处理每个客户端连接,而客户端则不断发送和接收数据。代码示例展示了服务器和客户端的交互过程。
摘要由CSDN通过智能技术生成

今天终于有时间来实践一下Java socket的使用方法了,需要注意几个问题:

(1)、使用socket.getInputStream 读取数据时,该方法是阻塞方法,即如果管道流中没有数据时,使用inputStream.read(byte)是会一直阻塞,所以如果想要使用inputStream.read(new byte[]) > 0 来结束

循环是行不通的;

(2)、由于我的目的是不停的交互数据,所以不能IO流关闭,也不能把socket会话关闭,否则数据交换就会停止。

贴代码做记录:

1、服务端代码:

package com.server;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.ServerSocket;

import java.net.Socket;

import com.base.Constant;

public class ServerMain {

/**

* @param args

*/

public static void main(String[] args) {

ServerSocket serverSocket = null;

System.out.println("ServerSocket Begin........");

int num = 0;

try {

serverSocket = new ServerSocket(Constant.PORT);

//使用循环方式一直等待客户端的连接

while(true){

num ++;

Socket accept = serverSocket.accept();

//启动一个新的线程,接管与当前客户端的交互会话

new Thread(new ServerThread(accept),"Client "+num).start();

}

} catch (IOException e) {

e.printStackTrace();

}

finally{

try {

serverSocket.close();

System.out.println("----> serverSocket closed.");

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

/**

* @author JCC

* 服务器处理客户端会话的线程

*/

class ServerThread implements Runnable {

Socket socket = null;

public ServerThread(Socket socket){

System.out.println("Create a new ServerThread...");

this.socket = socket;

}

@Override

public void run() {

InputStream in = null;

OutputStream out = null;

try {

in = socket.getInputStream();

out = socket.getOutputStream();

//使用循环的方式,不停的与客户端交互会话

while(true){

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

//处理客户端发来的数据

doRead(in);

System.out.println("send Message to client.");

//发送数据回客户端

doWrite(out);

}

} catch (IOException e) {

e.printStackTrace();

}

finally{

try {

in.close();

out.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

/**

* 读取数据

* @param in

* @return

*/

public boolean doRead(InputStream in){

//引用关系,不要在此处关闭流

try {

byte[] bytes = new byte[in.available()];

in.read(bytes);

System.out.println("line:"+new String(bytes).trim());

} catch (IOException e) {

e.printStackTrace();

}

return true;

}

/**

* 写入数据

* @param out

* @return

*/

public boolean doWrite(OutputStream out){

//引用关系,不要在此处关闭流

try {

out.write("welcome you client.".getBytes());

out.flush();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return true;

}

}

2、客户端代码:

package com.client;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

import java.net.UnknownHostException;

import com.base.Constant;

class ClientMain {

/**

* @param args

*/

public static void main(String[] args) {

Socket socket = null;

System.out.println("ClientSocket Begin........");

try {

for(int i = 0;i<5;i++){

socket = new Socket(Constant.SERVER_IP,Constant.PORT);

new Thread(new ClientThread(socket,i),"ClientThread "+i).start();

}

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 客户端线程

* @author JCC

*

*/

class ClientThread implements Runnable{

Socket socket = null;

int id = 0;

public ClientThread(Socket socket,int id){

this.socket = socket;

this.id = id;

}

@Override

public void run() {

OutputStream out = null;

InputStream in = null;

System.out.println("Begin to Chat to server...");

try {

out = socket.getOutputStream();

in = socket.getInputStream();

//循环发送与服务端不停的交互数据

while(true){

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

doWrite(out);

System.out.println("begin read message from server.");

doRead(in);

}

} catch (IOException e) {

e.printStackTrace();

}

finally{

try {

in.close();

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 读取服务端数据

* @param in

* @return

*/

public static boolean doRead(InputStream in){

//引用关系,不要在此处关闭流

byte[] bytes = new byte[1024];

try {

in.read(bytes);

System.out.println("line:"+new String(bytes).trim());

} catch (IOException e) {

e.printStackTrace();

}

return true;

}

/**

* 发送数据到服务端

* @param out

* @return

*/

public boolean doWrite(OutputStream out){

//引用关系,不要在此处关闭流

String line = "Hello server, I am client = "+id +"\n";

line = line +"I want you to do something for me";

try {

out.write(line.getBytes());

out.flush();

} catch (IOException e) {

e.printStackTrace();

}

return true;

}

}

3、常量类:

package com.base;

public interface Constant {

public static final String SERVER_IP = "192.168.1.44";

public static final int PORT = 4567;

}

执行结果:

(1)服务端结果:

ServerSocket Begin........

Create a new ServerThread...

Create a new ServerThread...

Create a new ServerThread...

Create a new ServerThread...

Create a new ServerThread...

line:

send Message to client.

line:

send Message to client.

line:

send Message to client.

line:Hello server, I am client = 3

I want you to do something for me

send Message to client.

line:Hello server, I am client = 4

I want you to do something for me

send Message to client.

line:Hello server, I am client = 0

I want you to do something for me

send Message to client.

line:Hello server, I am client = 1

I want you to do something for me

send Message to client.

line:Hello server, I am client = 2

I want you to do something for me

send Message to client.

line:

send Message to client.

line:

send Message to client.

line:Hello server, I am client = 0

I want you to do something for me

send Message to client.

line:Hello server, I am client = 1

I want you to do something for me

send Message to client.

line:Hello server, I am client = 2

I want you to do something for me

send Message to client.

line:Hello server, I am client = 3

I want you to do something for me

send Message to client.

line:Hello server, I am client = 4

I want you to do something for me

send Message to client.

line:Hello server, I am client = 0

I want you to do something for me

send Message to client.

(2)客户端结果:

ClientSocket Begin........

Begin to Chat to server...

Begin to Chat to server...

Begin to Chat to server...

Begin to Chat to server...

Begin to Chat to server...

begin read message from server.

line:welcome you client.

begin read message from server.

line:welcome you client.

begin read message from server.

line:welcome you client.

begin read message from server.

line:welcome you client.

begin read message from server.

line:welcome you client.

begin read message from server.

line:welcome you client.

begin read message from server.

line:welcome you client.

begin read message from server.

line:welcome you client.

begin read message from server.

line:welcome you client.

begin read message from server.

line:welcome you client.

begin read message from server.

line:welcome you client.

begin read message from server.

line:welcome you client.

begin read message from server.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值