Java怎么重复使用套接字_在java中连续地通过套接字传输数据
我想通過推送和拉動異步發送/接收從一個端點到另一個端點(peer2peer)的連續數據流
因此,為了首先解決通信,我開始使用jax-ws soap綁定webservice,因為它有一個端點和推送機制的ws-addressing,但它似乎是很多開銷(根據文檔很重,因為不熟悉ws- *,我沒有實現它,因為我需要多個客戶端稍后監聽流,并且流是24/7我想要線程可管理的套接字).
然后我拿了jax-rs但它不包含ws-addressing.(jax-rs 2.0)
我也看了websockets但它需要一個應用服務器,但我想要一個jvm支持代碼
所以,現在我正在嘗試使用基本套接字,但我遇到的問題是通過服務器上的套接字流式傳輸數據,客戶端不斷接收它.
這是第一次閱讀,但沒有進一步.
其次,我怎樣才能使它異步?
public class sSocket {
public static void main(String args[]) throws IOException{
int i = 15000;
ServerSocket ss;
Socket socket = null;
ss = new ServerSocket(i);
try
{
socket = ss.accept();
socket.setKeepAlive(true);
int iii = 0;
System.out.println("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while(iii<9)
{
Thread.sleep(2000);
output.write("good" + iii + "\n");
//System.out.print(input.readLine().toString());
output.flush();
iii++;
}
//socket.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
public class cSocket {
public static void main(String args[]) throws InterruptedException, IOException{
Socket client = new Socket("127.0.0.1", 15000);
try{
client.setKeepAlive(true);
DataOutputStream out = new DataOutputStream(client.getOutputStream());
out.writeBytes("Hi Server! I'm " + client.getLocalSocketAddress() + "\n" );
BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));
String s;
while(true){
if((s = input.readLine()) != null)
{
System.out.println("Message from Server: " + s);
}}
//client.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
[toString()不可用 – 沒有掛起的線程]我看到這停止了eclipse中的代碼.
問題似乎基本上是在客戶端的input.readLine()根源:錯誤是連接重置:我假設是因為readLine()已達到“EOF”
總結
以上是生活随笔為你收集整理的Java怎么重复使用套接字_在java中连续地通过套接字传输数据的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: clojure java.jdbc_Cl
 - 下一篇: 我的世界连锁挖矿下载JAVA_我的世界1