導航:首頁 > 編程語言 > socket監聽java

socket監聽java

發布時間:2023-05-05 12:34:13

java Web項目怎麼監聽socket請求

不知道具此陵耐體需求是什麼。如果用java web作為服務端,那可以汪褲在web項目啟動的時候,增加socket監聽服務。在web項目的listener增加啟動類。
或者乾脆使森春用如netty這樣的IO框架。

Ⅱ Java socket通信中,當伺服器端接受到監聽客戶端的請求後,如何主動給客戶端發送數據

ServerSocket ss = new ServerSocket();
Socket s = ss.accept();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write("hello!");

Ⅲ 如何用JAVA代碼監聽某個開放埠

比如我要監聽1234這個埠,代碼如下:
String ip = "127.0.0.1";
int port = 1234;
try {
Socket socket = new Socket(ip, port);
socket.setSoTimeout(5539900);
java.io.OutputStream out = socket.getOutputStream();

byte[] date = "hello world".getBytes();
out.write(data);
out.flush();
byte[] buffer = new byte[1024];
int len = -1;
java.io.FileOutputStream fout = new java.io.FileOutputStream(
"d:/response.txt");
java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
java.io.InputStream in = socket.getInputStream();
while ((len = in.read(buffer, 0, buffer.length)) > 0) {
bout.write(buffer, 0, len);
}
in.close();
bout.flush();
bout.close();

byte[] rdata = bout.toByteArray();
System.out.println(new String(rdata));

fout.write(rdata);
fout.flush();
fout.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

伺服器端的

ServerSocket ss = new ServerSocket(1234);
Socket socket=null;
BufferedReader in;
PrintWriter out;

while (true) {
socket = ss.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
String line = in.readLine();
out.println("you input is :" + line);
out.close();
in.close();
socket.close();
}

Ⅳ 請問在java中ServerSocket監聽埠時ip是什麼

四個0,說明是全部的IP都監聽

~

Ⅳ 如何使java socket伺服器端在接收到了客戶端的數據後還能夠處於監聽狀態不要籠統的答案。

ServerSocket的對象調用accept()方法進行阻塞等待連接。


如果想接念敬受數據並處激高啟於阻塞等等連接狀態,使用while(true)循環監聽明如。

while(true)
{
Sockets=ServerSocket_o.accept();//等待連接
Threadt=newSocketRead(s);//創建線程,SocketRead繼承Thread,並重寫run方法。
t.start();//啟動線程用於從已建立的連接接受和發送數據。

}

Ⅵ java war項目中使用socket監聽,重啟項目出現埠被tomcat佔用

埠號被佔了,去關閉佔用埠的程序,或者修改tomcat埠號

Ⅶ 如何監聽 java socket

String ip = "127.0.0.1";
int port = 1234;
try {
Socket socket = new Socket(ip, port);
socket.setSoTimeout(5539900);
java.io.OutputStream out = socket.getOutputStream();

byte[] date = "hello world".getBytes();
out.write(data);
out.flush();
byte[] buffer = new byte[1024];
int len = -1;
java.io.FileOutputStream fout = new java.io.FileOutputStream(
"d:/response.txt");
java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
java.io.InputStream in = socket.getInputStream();
while ((len = in.read(buffer, 0, buffer.length)) > 0) {
bout.write(buffer, 0, len);
}
in.close();
bout.flush();
bout.close();

byte[] rdata = bout.toByteArray();
System.out.println(new String(rdata));

fout.write(rdata);
fout.flush();
fout.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

伺服器端的

ServerSocket ss = new ServerSocket(1234);
Socket socket=null;
BufferedReader in;
PrintWriter out;

while (true) {
socket = ss.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
String line = in.readLine();
out.println("拿野you input is :"侍敏逗老賣 + line);
out.close();
in.close();
socket.close();
}

Ⅷ java Socket編程中,為什麼監聽多個埠時出現有一個埠無法接受到信息,需要用到線程嗎

您好,提問者:
其實線程的問題還是很難解決的,因為是隨機的 。

但是也不是沒有解決辦法,是這樣的,有些功能是必須用到線程的,如果不用線臘碼程的話,可能會有接收不到的數據、停止後台超時等錯誤,建議使用線程。。。
舉例:比如坦克大戰、撲輪讓哪克牌,不用線程是無法完成的,也不是無法完全,就是會數滑梁據接收不到。

Ⅸ java 如何在伺服器端用socket創建一個監聽埠,並對接受的數據進行處理,埠號為3333,請高手指點一下

我網路HI你好了

public class Test {

public static void main(String[] args) {
Test1 t=new Test1();
t.start(); //啟動線程
}

}
/**
* 繼承一個線程類
* @author Administrator
*
*/
class Test1 extends Thread{
private ServerSocket server = null;
public Test1(){
try {
server=new ServerSocket(3333);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("服務端初始化scoket失敗!");
}
}
/**
* 繼承父類方法
*/
public void run(){
Socket clientSocket = null;
DataInputStream dis =null;
DataOutputStream dos=null;
String str="";
while(true){
try {
clientSocket=server.accept();
dis = new DataInputStream(clientSocket.getInputStream());//獲取輸入流,用於接收客戶端發送來的數據
dos = new DataOutputStream(clientSocket.getOutputStream());//獲取輸出流,用於客戶端向伺服器端發送數據
str=dis.readUTF(); //這里是客戶端發送來的數據
/*
* 這里邊你就可以做你想操作的事情了
*/
dos.writeUTF("這里是返回到客戶端的數據");//這里用來向客戶端返回數據
dis.close();
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

Ⅹ java socket客戶端一直監聽服務端返回數據

監聽你系統通信所用的埠,有數據就獲取顯示就可以了。例如你的伺服器ip是168.12.1.16,使用的是2000埠號,Socket socket=new Socket("168.12.1.16",2000);
DataOutputStream in=new DataOutputStream(socket.getInputStream());

閱讀全文

與socket監聽java相關的資料

熱點內容
解壓玩具創意圖片 瀏覽:525
組態軟體pdf 瀏覽:484
箍筋加密區是如何計算根數的 瀏覽:852
筆記本如何伺服器從做raid 瀏覽:929
視頻編解碼演算法工程師 瀏覽:587
手機上伺服器怎麼找 瀏覽:323
抖音程序員變裝男 瀏覽:338
qd128y壓縮機參數 瀏覽:834
解壓棒的視頻 瀏覽:145
linux下kill命令 瀏覽:346
伺服器和網路機櫃有什麼重要 瀏覽:349
貼吧文件夾位置圖片 瀏覽:506
python讀取處理二進制文件 瀏覽:398
億速雲北京三區雲伺服器 瀏覽:566
高管綠程序員 瀏覽:43
蘿卜影視APP源碼推薦設置教程 瀏覽:782
小米官方包被加密 瀏覽:884
程序員的午飯視頻 瀏覽:289
雅思聽力pdf 瀏覽:359
高跟鞋先生程序員 瀏覽:59