TCP 简单通信作业 小白求助大佬

呵呵小哥 2019-04-06 07:31:32
编辑tcp的循环聊天一直出问题,希望大佬帮忙给看一下,万分感谢!
以下为代码:


服务端:
public class TCPServer {
public static void main(String[] args) {
ServerSocket ss = null;
Socket s = null;
BufferedReader br = null;
try {
ss = new ServerSocket(20000);
s = ss.accept();
InputStream in = s.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
String line = null;
InetAddress ia = s.getInetAddress();
while((line = br.readLine()) != null){
System.out.println(ia.getHostAddress() + "接受到了:" + line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null){
br.close();
}
if(s != null){
s.close();
}
if(ss != null){
ss.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


以下是客户端:

public class TCPClient {
public static void main(String[] args) {
Socket s = null;
BufferedWriter bw = null;
BufferedReader br = null;
try {
s = new Socket("192.168.2.105", 20000);
OutputStream out = s.getOutputStream();
bw = new BufferedWriter(new OutputStreamWriter(out));
br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(br != null){
br.close();
}
if(bw != null){
bw.close();
}
if(s != null){
s.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}


最后运行,服务端收到的一直是空白,,,
谢谢大佬了!!!
...全文
224 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39936465 2019-04-09
  • 打赏
  • 举报
回复
用BufferedWriter需要添加一句newLine(), 用printstream更灵活方便点。client 按下面改一下就行了 while((line = br.readLine()) != null){ bw.write(line); bw.newLine(); bw.flush(); }
qq_39936465 2019-04-09
  • 打赏
  • 举报
回复
建议服务器和客户端之间对话用PrintStream流,这样client可以改一下

public class TCPClient {
	public static void main(String[] args) {
		Socket s = null;
		PrintStream ps=null;
		BufferedReader br = null;
		try {
			s = new Socket("localhost", 20000);
			OutputStream out = s.getOutputStream();
			ps = new PrintStream(out);
			br = new BufferedReader(new InputStreamReader(System.in));
			String line = null;
			while ((line = br.readLine()) != null) {
				ps.println(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null) {
					br.close();
					s.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
用loaclhost测试没有问题。
qq_39936465 2019-04-09
  • 打赏
  • 举报
回复
我看了元程序应该是下面有问题,if条件是无法满足的,只要br关闭后跟着关闭bw即可。

if(bw != null){
bw.close();
}
qq_39936465 2019-04-08
  • 打赏
  • 举报
回复
引用 楼主 永以为依也 的回复:
编辑tcp的循环聊天一直出问题,希望大佬帮忙给看一下,万分感谢!
以下为代码:


服务端:
public class TCPServer {
public static void main(String[] args) {
ServerSocket ss = null;
Socket s = null;
BufferedReader br = null;
try {
ss = new ServerSocket(20000);
s = ss.accept();
InputStream in = s.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
String line = null;
InetAddress ia = s.getInetAddress();
while((line = br.readLine()) != null){
System.out.println(ia.getHostAddress() + "接受到了:" + line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null){
br.close();
}
if(s != null){
s.close();
}
if(ss != null){
ss.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


以下是客户端:

public class TCPClient {
public static void main(String[] args) {
Socket s = null;
BufferedWriter bw = null;
BufferedReader br = null;
try {
s = new Socket("192.168.2.105", 20000);
OutputStream out = s.getOutputStream();
bw = new BufferedWriter(new OutputStreamWriter(out));
br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(br != null){
br.close();
}
if(bw != null){
bw.close();
}
if(s != null){
s.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}


最后运行,服务端收到的一直是空白,,,
谢谢大佬了!!!



因为你客户端BufferedWrite 流一直没关闭,所以服务器一直处于等待中,这里flush不起效果。


while ((line = br.readLine()) != null) {
bw.write(line);
bw.close();
}


直接在while中关闭就行了。
要循环对话的话,服务器端也需要添加while循环。自己再改一下吧。
Dan淡淡的心 2019-04-07
  • 打赏
  • 举报
回复
首先确定一下 client 是否在读取数据 在循环内打印一下line 可以打印的话 单机测试一下 将 ip改为 127.0.0.1
三月の狮子 2019-04-07
  • 打赏
  • 举报
回复
line的值是null?
呵呵小哥 2019-04-06
  • 打赏
  • 举报
回复
引用 1 楼 老学徒的回复:
客户端加上 pw = printWriter(bw,true);
不行啊,打上了还是空白的
一克李普斯 2019-04-06
  • 打赏
  • 举报
回复
客户端加上 pw = printWriter(bw,true);

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧