用java的Socket写的流服务端接收不到客户端发送的信息。 40
importjava.net.*;importjava.io.*;publicclassTCPServer{publicstaticvoidmain(Stringargs...
import java.net.*;
import java.io.*;
public class TCPServer
{
public static void main(String args[])throws Exception{
ServerSocket server=new ServerSocket(30011);
Socket client=server.accept();
String ip=cilent.getInetAddress().getHostAddress();
System.out.println(ip+"...Contected");
BufferedReader bufIn=new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedWriter bufOut=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
String line=null;
while((line=bufIn.readLine())!=null){
System.out.println(line);
bufOut.write(line.toUpperCase());
bufOut.newLine();
}
client.close();
}
}
——————————————————————————————————————————
import java.net.*;
import java.io.*;
public class TCPClient
{
public static void main(String args[])throws Exception{
Socket client;
try{
client=new Socket("localhost",30011);
}catch(Exception e ){
throw new RuntimeException("Cilent contection feild");
}
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
BufferedReader bufIn=new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedWriter bufOut=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
String line=null;
while((line=bufr.readLine())!=null){
bufOut.write(line);
bufOut.newLine();
System.out.println("Sent to server");
String data=bufIn.readLine();
System.out.println("Server: "+data);
}
bufr.close();
client.close();
}
} 展开
import java.io.*;
public class TCPServer
{
public static void main(String args[])throws Exception{
ServerSocket server=new ServerSocket(30011);
Socket client=server.accept();
String ip=cilent.getInetAddress().getHostAddress();
System.out.println(ip+"...Contected");
BufferedReader bufIn=new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedWriter bufOut=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
String line=null;
while((line=bufIn.readLine())!=null){
System.out.println(line);
bufOut.write(line.toUpperCase());
bufOut.newLine();
}
client.close();
}
}
——————————————————————————————————————————
import java.net.*;
import java.io.*;
public class TCPClient
{
public static void main(String args[])throws Exception{
Socket client;
try{
client=new Socket("localhost",30011);
}catch(Exception e ){
throw new RuntimeException("Cilent contection feild");
}
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
BufferedReader bufIn=new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedWriter bufOut=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
String line=null;
while((line=bufr.readLine())!=null){
bufOut.write(line);
bufOut.newLine();
System.out.println("Sent to server");
String data=bufIn.readLine();
System.out.println("Server: "+data);
}
bufr.close();
client.close();
}
} 展开
3个回答
展开全部
//在server “写东西”的 while 外边,加上个flush。其他的不变。试一试。
while((line=bufIn.readLine())!=null){
System.out.println(line);
bufOut.write(line.toUpperCase());
bufOut.newLine();
}
bufOut.flush();//清空缓冲区。
while((line=bufIn.readLine())!=null){
System.out.println(line);
bufOut.write(line.toUpperCase());
bufOut.newLine();
}
bufOut.flush();//清空缓冲区。
追问
还是不行
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没出什么问题。把localhost 改为127.0.0.1试试
追问
还是不行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询