黑马程序员技术交流社区
标题:
不知道哪里错了,第二次键盘输入卡了!
[打印本页]
作者:
唐宋博宇
时间:
2013-3-16 19:38
标题:
不知道哪里错了,第二次键盘输入卡了!
package com.shanhw.fifthDay;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TestTCPChatSingleProcess {
/**
* TCP聊天
*/
public static void main(String[] args) throws InterruptedException {
TCPChatServer ts = new TCPChatServer();
new Thread(ts).start();
TCPChatClient tc = new TCPChatClient();
new Thread(tc).start();
}
}
/**
* TCP聊天:客户端
*/
class TCPChatClient implements Runnable{
public void run() {
try {
System.out.println("Client……");
Socket socket = new Socket("192.168.1.100", 10005);
// 键盘录入流
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
// 要写入服务端的输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream()));
String len = null;
while ((len = br.readLine()) != null) {
if ("over".equals(len)){
System.out.println("Client 关闭成功");
break;
}
bw.write(len);
bw.newLine();
bw.flush();
}
System.out.println("2");
// 在服务器端返回的信息
BufferedReader bufR = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
String serverMessage = bufR.readLine();
System.out.println("Sever Say :" + serverMessage);
br.close();
// 关闭服务端
socket.close();
} catch (Exception e) {
throw new RuntimeException("客户端运行时错误");
}
}
}
/**
* TCP聊天:服务端
*/
class TCPChatServer implements Runnable {
public void run() {
try {
System.out.println("Server……");
ServerSocket ss = new ServerSocket(10005);
// 得到客户端socket对象
Socket socket = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
String clientMessage = br.readLine();
System.out.println("Clinet Say:" + clientMessage);
// 键盘录入流
BufferedReader bufR = new BufferedReader(new InputStreamReader(
System.in));
// 要写入服务端的输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()));
String len = null;
while ((len = bufR.readLine()) != null) {
if ("over".equals(len)) {
System.out.println("Server 关闭成功");
break;
}
bw.write(len.toUpperCase());
bw.newLine();
bw.flush();
}
bufR.close();
// 关闭客户端,选择性关闭服务端
socket.close();
ss.close();
} catch (Exception e) {
throw new RuntimeException("服务端运行时错误");
}
}
}
复制代码
作者:
BitmapFactory
时间:
2013-3-16 22:41
本帖最后由 张学林 于 2013-3-16 22:52 编辑
不知道你的需求是什么,我把需求改成了:(可能改变了你的本意,可做参考)
客户端向服务端发送数据,服务端将收到的数据变成大写,再返给客户端
客户端将收到的数据打印在控制台上,当客户端输入over时,服务端将客户端连接关掉,客户端自己也关掉
你的问题出现在了标号<1>处(84行)导致49行和93行阻塞等待读取数据
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TestTCPChatSingleProcess {
/**
* TCP聊天
*/
public static void main(String[] args) throws InterruptedException {
TCPChatServer ts = new TCPChatServer();
new Thread(ts).start();
TCPChatClient tc = new TCPChatClient();
new Thread(tc).start();
}
}
/**
* TCP聊天:客户端
*/
class TCPChatClient implements Runnable {
public void run() {
try {
// System.out.println("Client……");
Socket socket = new Socket("127.0.0.1", 10005);
// 键盘录入流
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
// 要写入服务端的输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()));
//定义一个接收服务端数据的缓冲区
BufferedReader br2 = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String len = null;
String len2 = null;
while ((len = br.readLine()) != null) {//<1>处错误,没有返回给客户端任何数据,使客户端和服务端都等待读取数据
if ("over".equals(len)) {
System.out.println("Client 关闭成功");
break;
}
bw.write(len);
bw.newLine();
bw.flush();
len2 = br2.readLine();// 接收服务器转换后的数据
System.out.println(len2 + ".........这是服务端转好的数据");
}
socket.shutdownOutput();
System.out.println("2");
// 在服务器端返回的信息
BufferedReader bufR = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String serverMessage = bufR.readLine();
System.out.println("Sever Say :" + serverMessage);
br.close();
// 关闭服务端
socket.close();
} catch (Exception e) {
throw new RuntimeException("客户端运行时错误");
}
}
}
/**
* TCP聊天:服务端
*/
class TCPChatServer implements Runnable {
public void run() {
try {
System.out.println("Server……");
ServerSocket ss = new ServerSocket(10005);
// 得到客户端socket对象
Socket socket = ss.accept();
System.out.println(socket.getInetAddress().getHostAddress()
+ "......connection");
BufferedReader br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
// String clientMessage = br.readLine();//<1>错误出在这里了当服务端接收这个之后,
// System.out.println("Clinet Say:" + clientMessage);
// 键盘录入流
// BufferedReader bufR = new BufferedReader(new InputStreamReader(
// System.in));
// 要写入服务端的输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()));
String len = null;
while ((len = br.readLine()) != null) {//<1>处错误,没有返回给客户端任何数据,使客户端和服务端都等待读取数据
if ("over".equals(len)) {
System.out.println("Server 关闭成功");
break;
}
System.out.println(len + ".........这是从客户端接收的数据");
bw.write(len.toUpperCase());
bw.newLine();
bw.flush();
}
bw.write("接收数据完毕,服务端将干掉客户端!!");
bw.newLine();
bw.close();
br.close();
// 关闭客户端,选择性关闭服务端
socket.close();
ss.close();
} catch (Exception e) {
throw new RuntimeException("服务端运行时错误");
}
}
}
复制代码
作者:
唐宋博宇
时间:
2013-3-16 23:35
张学林 发表于 2013-3-16 22:41
不知道你的需求是什么,我把需求改成了:(可能改变了你的本意,可做参考)
客户端向服务端发送数据,服务 ...
很感谢你的耐心回复,毕向东老师的网络编程视频我也看了 嘿嘿、、、你的指正很好、、、不过 我想要的是TCP:聊天(服务端和客户端的聊天),我已经修改好了,代码,你看看
package com.shanhw.fifthDay;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TestTCPChatSingleProcess {
/**
* TCP:聊天
*/
public static void main(String[] args) {
TCPChatServer ts = new TCPChatServer();
new Thread(ts).start();
TCPChatClient tc = new TCPChatClient();
new Thread(tc).start();
}
}
/**
* TCP:客户端
*/
class TCPChatClient implements Runnable{
public void run() {
try {
System.out.println("Client……");
Socket socket = new Socket("192.168.1.100", 10005);
// 键盘录入流
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
// 要写入服务端的输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream()));
// 定义socket读取流,服务器端返回的信息
BufferedReader bufR = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
String len = null;
while ((len = br.readLine()) != null) {
if ("over".equals(len)){
System.out.println("Client 关闭成功");
break;
}
bw.write(len);
bw.newLine();
bw.flush();
String serverMessage = bufR.readLine();
System.out.println("Sever Say :" + serverMessage);
}
br.close();
// 关闭服务端
socket.close();
} catch (Exception e) {
throw new RuntimeException("客户端运行时错误");
}
}
}
/**
* TCP:服务端
*/
class TCPChatServer implements Runnable{
public void run() {
try {
System.out.println("Server……");
ServerSocket ss = new ServerSocket(10005);
// 得到客户端socket对象
Socket socket = ss.accept();
// socket读取流
BufferedReader bufIn = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
// 要写入服务端的输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()));
// 键盘录入流
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String len = null;
while ((len = bufIn.readLine()) != null) {
System.out.println("Clinet Say:" + len);
if ("over".equals(len)) {
System.out.println("Server 关闭成功");
break;
}
bw.write(br.readLine());
bw.newLine();
bw.flush();
}
bufIn.close();
// 关闭客户端,选择性关闭服务端
socket.close();
ss.close();
} catch (Exception e) {
throw new RuntimeException("服务端运行时错误");
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2