/*
这一个TCP协议的客服端与服务端通过键盘录入的方式将客户端的信息发给服务端的,为什么我客户端写了一句话后程序就死掉了双方都不动了,
我是按照毕老师视频里的步骤写的,为什么我的就会出问题呢,而且我还对着源码看了几遍都没发现问题,
将这个2个类的代码看了一整天,也不知道到底是哪里出的问题,气得快崩溃了,求高手指点一下!谢谢了
*/
import java.io.*;
import java.net.*;
class TcpClient4
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("127.0.0.1",10006);
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufOut=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while ((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
bufOut.write(line);
bufOut.newLine();
bufOut.flush();
String str=bufIn.readLine();
System.out.println("server:"+str);
}
bufr.close();
s.close();
}
}
class TcpServer4
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10006);
Socket s=ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"....connected");
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bufOut=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line=null;
while((line=bufIn.readLine())!=null);
{
System.out.println(line);
bufOut.write(line.toUpperCase());
bufOut.newLine();
bufOut.flush();
}
s.close();
ss.close();
}
} |