问题描述
解决方案
while循环会结束的,InputStream流输入到达尾部时,br.read(buf)返回的就是-1了,就标识输入结束。
这个是由客户端发送的Socket决定的,客户端Socket发送完毕,这段代码读取发送数据也就结束了。
解决方案二:
客户端发送了什么内容?正常情况下可以打印出客户端发送过来的数据,打印完就结束了。另外,客户端发送完数据要给一个结束标记。
解决方案三:
服务器端
import java.net.*;
import java.io.*;
public class Server
{
public static void main(String[] args) throws Exception
{
ServerSocket server = new ServerSocket(8888);
System.out.println("Wait for connection...");
Socket socket = server.accept();
BufferedInputStream is = new BufferedInputStream(socket.getInputStream());
byte[] buff = new byte[1024];
int size;
while((size = is.read(buff)) != -1){
System.out.println(new String(buff,0,size));
}
}
}
客户端
import java.net.*;
import java.io.*;
public class Client
{
public static void main(String[] args) throws Exception
{
Socket socket = new Socket("localhost",8888);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
byte[] buff = new byte[2048];
for(int i = 0 ; i < 2048 ; i ++){
buff[i] = 'a';
}
out.write(buff);
out.flush();//你可能没有写这个,我测试的时候,没写会出现socketexception:connection reset异常
out.close();
socket.close();
System.out.println("Send Over!");
}
}
解决方案四:
对应的客户端写入刘未关闭导致另一端等待输入流read阻塞了。解决办法:
服务器端:
ServerSocket server;
try {
server = new ServerSocket(8800);
System.out.println("Wait for connection...");
Socket socket = server.accept();
BufferedInputStream is = new BufferedInputStream(
socket.getInputStream());
byte[] buff = new byte[1024];
int size;
while ((size = is.read(buff)) != -1) {
System.out.println(new String(buff, 0, size));
}
OutputStream os = socket.getOutputStream();
os.write("您好,我是Server端".getBytes());
os.flush();
os.close();
System.out.println("ok,client send over.");
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
客户端端:
public static void main(String[] args) throws Exception
{
Socket s = new Socket("localhost",8800);
OutputStream os = s.getOutputStream();
os.write("您好,我是客户端".getBytes());
os.close();
InputStream in = s.getInputStream();
byte[] buff = new byte[1024];
BufferedInputStream is = new BufferedInputStream(in);
int size;
while((size = is.read(buff)) != -1){
System.out.println(new String(buff,0,size));
}
System.out.println("over...");
s.close();
}
解决方案五:
服务器
import java.net.*;
import java.io.*;
public class Server
{
public static void main(String[] args) throws Exception
{
ServerSocket server = new ServerSocket(8888);
System.out.println("Wait for connection...");
Socket socket = server.accept();
BufferedInputStream is = new BufferedInputStream(socket.getInputStream());
byte[] buff = new byte[1024];
int size;
//当客户端的输出流没有close时,服务器端收不到-1,会一直在沉睡在while里面,
while((size = is.read(buff)) != -1){
String str = new String(buff,0,size);
System.out.println(str);
/****************************************************/
if(str.indexOf('n') > 0){//存在消息结束标志
break;
}
/*****************************************************/
}
//is.close();
System.out.println("Received");
System.out.println("Sending...");
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
out.write("你好,我是服务器".getBytes());
out.flush();
out.close();
is.close();
socket.close();
System.out.println("Send Over!");
}
}
客户端
import java.net.*;
import java.io.*;
public class Client
{
public static void main(String[] args) throws Exception
{
Socket socket = new Socket("localhost",8888);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
/********************表示发送结束*****************************/
out.write("你好,我是客户端n".getBytes());
out.flush();
//out.close();有这句话时,服务器可以读到一个-1
System.out.println("Send Over!");
System.out.println("Receiving...");
BufferedInputStream is = new BufferedInputStream(socket.getInputStream());
byte[] buff = new byte[1024];
int size;
while((size = is.read(buff)) != -1){
System.out.println(new String(buff,0,size));
}
is.close();
out.close();
socket.close();
System.out.println("Received!");
}
}
时间: 2024-11-01 23:59:50