黑马程序员技术交流社区
标题:
TCP问题
[打印本页]
作者:
李东梁
时间:
2014-3-29 21:01
标题:
TCP问题
package cn.itcast.net.TPC;
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;
public class TransClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s=new Socket("127.0.0.1",10014);
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//PrintWriter out =new PrintWriter(s.getOutputStream(),true);
BufferedReader bufrin=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while ((line=bufr.readLine())!=null) {
if("over".equals(line))
break;
bufw.write(line);
String str=bufrin.readLine();
System.out.println("转换后"+str);
}
s.close();
}
}
复制代码
package cn.itcast.net.TPC;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TransServer {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(10014);
Socket s=ss.accept();
System.out.println(s.getInetAddress().getHostName());
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
String line=null;
while ((line=bufIn.readLine())!=null) {
out.println(line.toUpperCase());
}
s.close();
ss.close();
}
}
复制代码
做了一个,TCP协议的服务器客户端,服务器只负责把输入的字母转换成大写,我这样写不能翻译,错在了哪?
作者:
╰つ
时间:
2014-3-29 22:12
噢耶,总算把问题调出来了,好长时间没玩这个东西了,太坑了,原因是你没有使用flush把它刷到服务端去,所以服务端读取不到你的数据,所以你的客户端也不会收到数据,修正代码如下:
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;
public class stock {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s=new Socket("127.0.0.1",10014);
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//PrintWriter out =new PrintWriter(s.getOutputStream(),true);
BufferedReader bufrin=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
StringBuffer sb=new StringBuffer();
while ((line=bufr.readLine())!=null) {
sb.append(line+"\n");
if("over".equals(line)){
System.out.println(123);
break;
}
}
bufw.write(sb.toString());
bufw.flush();
String str=bufrin.readLine();
System.out.println("xixixiixxiixixix");
System.out.println("转换后"+str);
s.close();
}
}
复制代码
package stock;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class server {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(10014);
Socket s=ss.accept();
System.out.println(s.getInetAddress().getHostName());
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
String line=null;
while ((line=bufIn.readLine())!=null) {
out.println(line.toUpperCase());
}
out.close();
s.close();
ss.close();
}
}
复制代码
作者:
╰つ
时间:
2014-3-29 22:17
不好意思,一些调试语句没有去,还有你的程序有一些小问题,你发现没你客户端只能读取一行,不知道你是这样设计的,还是另有原因
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2