问题描述
- java server端发送数据给客户端出现问题,求高手帮忙看下!
-
java server端发送数据给客户端出现问题,求高手帮忙看下!
package com.zeph.multiclient;import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;public class MultiThreadServer extends Thread {
private Socket client;public MultiThreadServer(Socket c) { this.client = c; } public void run() { try { BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); OutputStreamWriter out = new OutputStreamWriter(client.getOutputStream()); //BufferedReader sin=new BufferedReader(new InputStreamReader(System.in)); //out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); String str = in.readLine();//接收信息 while (!(str.equals(null))) { System.out.println(str);//将接收到的信息打印出来 out.write('0');//信息写入输出流 out.flush();//立刻刷新输出流 System.out.println("ok"); str=null;//数据清空,重新接收数据 str = in.readLine();//接收信息 System.out.println(str);//将接收到的信息打印出来 str=null;//清空数据,以跳出循环 } } catch (IOException e) { } } public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(1234); while (true) { MultiThreadServer mc = new MultiThreadServer(server.accept()); mc.start(); } }
}
解决方案
你这没头没尾的没法看,建议你找一个完整的例子学习
http://blog.csdn.net/xiangqiao123/article/details/6844155
你先调试通,如果还不行,是你网络配置的问题,比如防火墙等。等你把它调试通了,再逐步按照你的要求完善。
解决方案二:
str=null;//清空数据,以跳出循环,不能这么写的,会报空指针。改成while(str!=null)
时间: 2024-10-10 05:47:17