客户端和服务器的通信出错了,希望给个指点谢谢

_Dust_小德 2013-08-18 05:16:02
/*
Server
*/

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerCircle {
public static void main(String[] args) {

ServerSocket ss = null;
Socket s = null;
BufferedReader bufin = null;
BufferedWriter bufout = null;
String str = null;
try {
ss = new ServerSocket(10004);
System.out.println("服务器等待连接...");
s = ss.accept();
System.out.println("连接服务器成功!");

while (true) {
bufin = new BufferedReader(new InputStreamReader(
s.getInputStream()));
bufout = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
str = bufin.readLine();// 服务器在此停止
if (str.equalsIgnoreCase("exit")) {
bufout.write("Bye");
break;
} else {
Double radius = Double.parseDouble(str);
bufout.write("球的半径是:" + radius + "\n" + "球的体积是:" + 4.0 / 3
* Math.PI * Math.pow(radius, 3));
bufout.flush();
}
}
} catch (IOException e) {
} finally {
try {
bufin.close();
} catch (IOException e) {
}
try {
bufout.close();
} catch (IOException e) {
}
try {
s.close();
} catch (IOException e) {
}
try {
ss.close();
} catch (IOException e) {
}
}
}
}
//-------------------------------------------------------------
/*
Client
*/

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class ClientCircle {
public static void main(String[] args) {

Socket s = null;
BufferedReader bufin = null;
BufferedWriter bufout = null;
String str = null;
Scanner sc = new Scanner(System.in);
try {
System.out.println("客户端请求计算服务器...");
s = new Socket("127.0.0.1", 10004);
System.out.println("请求成功!");
System.out.println("输入数据请求服务器计算,输入\"exit\"退出程序!");
while (true) {
bufin = new BufferedReader(
new InputStreamReader(s.getInputStream()));
bufout = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));

System.out.print("请输入一个半径:");
str = sc.nextLine();
if (!str.equalsIgnoreCase("exit")) {
bufout.write(str);
bufout.flush();
System.out.println(bufin.readLine());//程序阻塞在此
} else {
bufout.write("exit");
bufout.flush();
System.out.println(bufin.readLine());//程序阻塞在此
break;
}
}
} catch (UnknownHostException e) {
} catch (IOException e) {
} finally {

try {
bufout.close();
} catch (IOException e) {
}
try {
bufin.close();
} catch (IOException e) {
}
try {
s.close();
} catch (IOException e) {
}
}
}
}
/*程序编译没有任何错误就是在获取不到输入流*/
...全文
162 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
_jerrytiger 2013-08-19
  • 打赏
  • 举报
回复
readLine方法要读满一行才会返回,也就是要碰到\r\n字符出现,它才会返回,否则就会一直在那里阻塞,直到读到了\r\n。 所以,你在使用write方法时,应该在你发送的字符串后面加上\r\n 。 例如:bufout.write(str + “\r\n”);
_jerrytiger 2013-08-19
  • 打赏
  • 举报
回复

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerCircle {
public static void main(String[] args) {

ServerSocket ss = null;
Socket s = null;
BufferedReader bufin = null;
BufferedWriter bufout = null;
String str = null;
try {
ss = new ServerSocket(10004);
System.out.println("服务器等待连接...");
s = ss.accept();
System.out.println("连接服务器成功!");

while (true) {
bufin = new BufferedReader(new InputStreamReader(
s.getInputStream()));
bufout = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
str = bufin.readLine();// 服务器在此停止
if (str.equalsIgnoreCase("exit")) {
bufout.write("Bye");
break;
} else {
Double radius = Double.parseDouble(str);
bufout.write("球的半径是:" + radius + "\n" + "球的体积是:" + 4.0 / 3
* Math.PI * Math.pow(radius, 3));
bufout.flush();
}
}
} catch (IOException e) {
} finally {
try {
bufin.close();
} catch (IOException e) {
}
try {
bufout.close();
} catch (IOException e) {
}
try {
s.close();
} catch (IOException e) {
}
try {
ss.close();
} catch (IOException e) {
}
}
}
}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class ClientCircle {
public static void main(String[] args) {

Socket s = null;
BufferedReader bufin = null;
BufferedWriter bufout = null;
String str = null;
Scanner sc = new Scanner(System.in);
try {
System.out.println("客户端请求计算服务器...");
s = new Socket("127.0.0.1", 10004);
System.out.println("请求成功!");
System.out.println("输入数据请求服务器计算,输入\"exit\"退出程序!");
while (true) {
bufin = new BufferedReader(
new InputStreamReader(s.getInputStream()));
bufout = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));

System.out.print("请输入一个半径:");
str = sc.nextLine();
if (!str.equalsIgnoreCase("exit")) {
bufout.write(str);
bufout.flush();
System.out.println(bufin.readLine());//程序阻塞在此
} else {
bufout.write("exit");
bufout.flush();
System.out.println(bufin.readLine());//程序阻塞在此
break;
}
}
} catch (UnknownHostException e) {
} catch (IOException e) {
} finally {

try {
bufout.close();
} catch (IOException e) {
}
try {
bufin.close();
} catch (IOException e) {
}
try {
s.close();
} catch (IOException e) {
}
}
}
}
nemesis_ 2013-08-19
  • 打赏
  • 举报
回复
大概看了一下,写入数据时都用的bufout.write,也没加换行,但读取的时候又是用的readLine,readLine要看到换行才返回,没读到换行,就会出现这种停止的情况了。
可乐罐 2013-08-19
  • 打赏
  • 举报
回复
System.out.println(bufin.readLine());//程序阻塞在此 ---------------------------------------------------- 貌似这个只是读一行,不会阻塞的吧

62,621

社区成员

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

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