解决Java中socket使⽤getInputStream()阻塞问题⽬录
socket使⽤getInputStream()阻塞
⽤线程解决Socket的getInputStream阻塞
1.背景
2.问题
3.原因
4.解决办法
5.Socket通信注意事项
socket使⽤getInputStream()阻塞
今天⽤socket进⾏编程练习时,发现程序到了getInputStream()这⾥就进⾏不下去了
Socket socket = new Socket("127.0.0.1", 800);
ObjectInputStream reader = new InputStream());
System.out.println("a");
ObjectOutputStream writer = new OutputStream());
就这样的⼀个测试代码,a不会打印出来
后来发现是getInputStream()会⼀直阻塞在那⾥阻塞
我把两⾏代码调了⼀下就好了,还不太清楚原因,先记下来
Socket socket = new Socket("127.0.0.1", 800);
ObjectOutputStream writer = new OutputStream());
System.out.println("a");
ObjectInputStream reader = new InputStream());
⽤线程解决Socket的getInputStream阻塞
1.背景
在Socket通信中,当我们希望传输对象时,往往会⽤到输⼊/输出对象流。
ObjectInputStream in=new InputStream());
ObjectOutputStream out=new OutputStream());
2.问题
当程序调⽤InputStream()程序被被卡住。
3.原因
public ObjectInputStream(InputStream in) throws IOException的官⽅API显⽰:
Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectO
utputStream has written and flushed the header. [1]
4.解决办法
⽤线程的⽅式处理输⼊流。以下为⽰例代码:
//===============客户端代码 SocketClient.java=====================
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SocketClient {
private Socket socket;
private ObjectOutputStream out;
private ObjectInputStream in;
public SocketClient(){
try {
socket=new Socket("localhost",8081);
out=new OutputStream());  ReadThread readThread=new ReadThread();
readThread.start();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(String msg){
System.out.println("send message:"+msg);
try {
out.writeObject(msg);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
class ReadThread extends Thread{
boolean runFlag=true;
public void run(){
try {
in=new InputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
while(runFlag){
if(socket.isClosed()){
return;
}
try {
Object adObject();
if(obj instanceof String){
System.out.println("Client recive:"+obj);
}
}
catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
public void exit(){
runFlag=false;
}
}
public static void main(String[] args) {
SocketClient socketClient=new SocketClient();
System.out.println("build socketClient");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
socketClient.sendMessage("Hello first.");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
socketClient.sendMessage("Hello second.");
//============服务器端代码 SocketService.java=========== import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.ServerSocket;
import java.Socket;
import java.SocketException;
import java.util.Date;
public class SocketService {
ServerSocket serverSocket;
public SocketService(){
try {
serverSocket=new ServerSocket(8081);
while(true){
Socket socket=serverSocket.accept();
SocketServiceThread sst=new SocketServiceThread(socket);
sst.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class SocketServiceThread extends Thread{
Socket socket;
ObjectInputStream in;
ObjectOutputStream out;
boolean runFlag=true;
public SocketServiceThread(Socket socket){
if(null==socket){
runFlag=false;
return;
}
this.socket=socket;
try {
out=new OutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
if(null==socket){
System.out.println("socket is null");
return;
}
try {
in=new InputStream());
socket通信报文格式
while(runFlag){
if(socket.isClosed()){
System.out.println("socket is closed");
return;
}
try {
String obj=(adObject();
if(obj instanceof String){
System.out.println("Server recive:"+obj);
Date date=new Date();
out.writeObject("["+date+"]"+obj);
out.flush();
}
else{
System.out.println("Server recive:"+obj);
}
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SocketException e){
e.printStackTrace();
return;
catch (IOException e){
e.printStackTrace();
}
}
} catch (IOException e1) {
e1.printStackTrace();
return;
} catch (Exception e){
return;
}
}
public void exit(){
runFlag=false;
}
}
public static void main(String[] args) {
System.out.println("===============start service===============");
new SocketService();
}
}
5.Socket通信注意事项
(1).writeXXX()⽅法后⼀般⽤flush()来把缓存内容发送出去。
(2).发送对象时,对象必须串⾏化,即该对象需要实现Serializable接⼝。以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。