为什么Server端收到的数据只是一个?
在TCPClient端我用的是while(true),应该是隔一秒钟收到一个的呀我的程序流程是这样的:
首先,创建NewsModule nm对象和TCPClient tcpClient对象,
其次,tcpClient.talk(nm.toString())发送数据到TcpServerAgent端
再次,TcpServerAgent端接收数据,并且将数据打印以下是我的代码NewsModule.java
public class NewsModule{
String name;
int ID;
NewsModule(String name, int ID) {
this.name = name;
this.ID = ID;
} public String toString() {
String str = null;
str = String.format("<module><mid>%d<mid><mname>%s</mname><module>", ID, name);
return str;
}
public static void main(String[] args) throws IOException, InterruptedException {
NewsModule nm = new NewsModule("新闻模块", 1);
TCPClient tcpClient = new TCPClient();
tcpClient.talk(nm.toString());
}
}TCPClient.java文件
public class TCPClient {
int port = 5888;
SocketChannel socketChannel = null;
String string = "127.0.0.1";
public TCPClient() throws IOException {
socketChannel = SocketChannel.open();
InetAddress ia = InetAddress.getLocalHost();
InetSocketAddress isa = new InetSocketAddress(ia, port);
socketChannel.connect(isa);//与服务器建立连接
}
public void talk(String str) throws InterruptedException, IOException {
PrintWriter pw = getWriter(socketChannel.socket());
while(true) {
Thread.sleep(1000);
pw.println(str);
}
} private PrintWriter getWriter(Socket socket) throws IOException {
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut, true);
}
}用于接收的TcpServerAgent.java文件public class TcpServerAgent {
private int port = 5888;
private ServerSocketChannel serverSocketChannel = null;
private ExecutorService executorService;
private static final int POOL_MULTIPLE = 4; //单个CPU线程池中工作线程的数目
public TcpServerAgent() throws IOException {
//创建一个线程池
executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_MULTIPLE);
//创建一个ServiceSocketChannel对象
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().setReuseAddress(true);//此函数要在ServerSocket绑定到一个端口之前使用才有效
//把服务器进程与一个本地端口绑定
serverSocketChannel.socket().bind(new InetSocketAddress(port));
System.out.println("服务器启动...");
}
public void service() {
while(true) {
SocketChannel socketChannel = null;
try {
socketChannel = serverSocketChannel.accept();//接受到此通道套接字的连接。
executorService.execute(new Handler(socketChannel));//处理客户连接
} catch(IOException e) {
e.printStackTrace();
}
}
}class Handler implements Runnable {
private SocketChannel socketChannel;
public Handler(SocketChannel socketChannel) {
this.socketChannel = socketChannel;
}
public void run() {
try {
handle(socketChannel);
} catch (IOException e) {
e.printStackTrace();
}
}
public void handle(SocketChannel socketChannel) throws IOException {
String str = null;
Socket socket = socketChannel.socket();
BufferedReader br = getReader(socket);
str = br.readLine();//将接收到的数据进行处理,包括数据的更新,以及将数据进行打包。将其传到一个数据对象中
System.out.println(str);
}
private BufferedReader getReader(Socket socket) {
InputStream socketIn= null ;
try {
socketIn = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return new BufferedReader(new InputStreamReader(socketIn));
}
} public static void main(String[] args) throws IOException {
TcpServerAgent tsa = new TcpServerAgent();
tsa.service();
}
}
在TCPClient端我用的是while(true),应该是隔一秒钟收到一个的呀我的程序流程是这样的:
首先,创建NewsModule nm对象和TCPClient tcpClient对象,
其次,tcpClient.talk(nm.toString())发送数据到TcpServerAgent端
再次,TcpServerAgent端接收数据,并且将数据打印以下是我的代码NewsModule.java
public class NewsModule{
String name;
int ID;
NewsModule(String name, int ID) {
this.name = name;
this.ID = ID;
} public String toString() {
String str = null;
str = String.format("<module><mid>%d<mid><mname>%s</mname><module>", ID, name);
return str;
}
public static void main(String[] args) throws IOException, InterruptedException {
NewsModule nm = new NewsModule("新闻模块", 1);
TCPClient tcpClient = new TCPClient();
tcpClient.talk(nm.toString());
}
}TCPClient.java文件
public class TCPClient {
int port = 5888;
SocketChannel socketChannel = null;
String string = "127.0.0.1";
public TCPClient() throws IOException {
socketChannel = SocketChannel.open();
InetAddress ia = InetAddress.getLocalHost();
InetSocketAddress isa = new InetSocketAddress(ia, port);
socketChannel.connect(isa);//与服务器建立连接
}
public void talk(String str) throws InterruptedException, IOException {
PrintWriter pw = getWriter(socketChannel.socket());
while(true) {
Thread.sleep(1000);
pw.println(str);
}
} private PrintWriter getWriter(Socket socket) throws IOException {
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut, true);
}
}用于接收的TcpServerAgent.java文件public class TcpServerAgent {
private int port = 5888;
private ServerSocketChannel serverSocketChannel = null;
private ExecutorService executorService;
private static final int POOL_MULTIPLE = 4; //单个CPU线程池中工作线程的数目
public TcpServerAgent() throws IOException {
//创建一个线程池
executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_MULTIPLE);
//创建一个ServiceSocketChannel对象
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().setReuseAddress(true);//此函数要在ServerSocket绑定到一个端口之前使用才有效
//把服务器进程与一个本地端口绑定
serverSocketChannel.socket().bind(new InetSocketAddress(port));
System.out.println("服务器启动...");
}
public void service() {
while(true) {
SocketChannel socketChannel = null;
try {
socketChannel = serverSocketChannel.accept();//接受到此通道套接字的连接。
executorService.execute(new Handler(socketChannel));//处理客户连接
} catch(IOException e) {
e.printStackTrace();
}
}
}class Handler implements Runnable {
private SocketChannel socketChannel;
public Handler(SocketChannel socketChannel) {
this.socketChannel = socketChannel;
}
public void run() {
try {
handle(socketChannel);
} catch (IOException e) {
e.printStackTrace();
}
}
public void handle(SocketChannel socketChannel) throws IOException {
String str = null;
Socket socket = socketChannel.socket();
BufferedReader br = getReader(socket);
str = br.readLine();//将接收到的数据进行处理,包括数据的更新,以及将数据进行打包。将其传到一个数据对象中
System.out.println(str);
}
private BufferedReader getReader(Socket socket) {
InputStream socketIn= null ;
try {
socketIn = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return new BufferedReader(new InputStreamReader(socketIn));
}
} public static void main(String[] args) throws IOException {
TcpServerAgent tsa = new TcpServerAgent();
tsa.service();
}
}
public void service() {
while(true) {
SocketChannel socketChannel = null;
try {
socketChannel = serverSocketChannel.accept();//接受到此通道套接字的连接。
executorService.execute(new Handler(socketChannel));//处理客户连接
} catch(IOException e) {
e.printStackTrace();
}
}
}
放在run()方法里面,才会一直监听接收。
我在handle中写了一个while(true)就解决了