導航:首頁 > 編程語言 > javasocket客戶端

javasocket客戶端

發布時間:2025-02-09 16:05:11

java socket如何實現客戶端與客戶端的交互

1、服務端監聽一個埠,其它客戶端都通過這個埠和服務端進行通信。
2、每個客戶端連接上來,服務端給其一個標識ID。然後向其它所有客戶端廣播一下有新客戶端接入,ID多少。
3、客戶端要向客戶端發送消息,可以以消息包的形式發送,就是把目的客戶端的標識和發送的內容組成一個數據包發往伺服器,伺服器讀取就知道要向哪 個客戶端發送數據,然後把內容往目的客戶端通道發送

❷ JAVA 問題,用socket編寫一個客戶端程序

給你一個我寫的示例,用的是基於TCP的Socket技術,你鍛煉一下,改一改,不會改再找我!
客戶端:
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

public class TcpClient {

public static void main(String[] args) throws Exception
{
//創建客戶端Socket服務,並且制定主機和ank
Socket s = new Socket("192.168.1.104",10002);//連接固定的主機和埠

//為了發送數據,獲取Socket中的輸入輸出流
OutputStream out = s.getOutputStream();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String line = null;

//讀取伺服器發過來的數據
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];

while((line = br.readLine())!=null)
{
out.write(line.getBytes());
if("886".equals(line))
break;

int len = in.read(buf);
String content = new String(buf,0,len);
System.out.println("Server:"+content);
}

s.close();

}
}

伺服器:
/*
* 需求分析:
* 使用TCP協議,寫伺服器端。做到伺服器能收到客戶端的信息,也能向客戶端發送信息
* */

package JavaNetProgramming;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

public class TcpServer {
public static void main(String[] args) throws Exception
{
//建立伺服器的Socket服務,並且監聽一個埠
ServerSocket ss = new ServerSocket(10002);
Socket s = ss.accept();
InputStream is = s.getInputStream();

//從伺服器端向客戶端發送數據
OutputStream out = s.getOutputStream();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String line = null;

while(true)
{
//通過accept()方法獲得客戶端傳過來的Socket對象

// System.out.println("getByNanme():"+s.getInetAddress());
System.out.print("getHostAddress():"+s.getInetAddress().getHostAddress());

//獲取客戶端發過來的數據,就要使用客戶端對象的讀取流來讀取數據

byte[] buf = new byte[1024];
int len = is.read(buf);//把流中數據讀到位元組數組中
String content = new String(buf,0,len);
System.out.println(" "+content);

if("886".equals(content))
break;
while((line = br.readLine())!=null)
{
out.write(line.getBytes());
break;
}

}

s.close(); //循環內有判斷語句,這句話才不出錯
ss.close();

}

}

❸ java中用socket實現客戶端與服務端雙向連接問題

//服務端程序:
importjava.io.*;
importjava.net.*;

publicclassTCPServer{
publicstaticvoidmain(String[]args)throwsIOException{
().init();
}
@SuppressWarnings("static-access")
privatevoidinit()throwsIOException{
@SuppressWarnings("resource")
ServerSocketserver=newServerSocket(1000);
Socketclient=null;
while(true){
try{
client=server.accept();
BufferedInputStreambis=newBufferedInputStream(client.getInputStream());
byte[]b=newbyte[1024];
intlen=0;
Stringmessage="";
while((len=bis.read(b))!=-1){
message=newString(b,0,len);
System.out.print("客戶端:"+client.getInetAddress().getLocalHost().getHostAddress()+"發來消息:"+message);
if("byte".equals(message.trim()))
client.close();
PrintWriterpw=newPrintWriter(client.getOutputStream(),true);
pw.println(message);
}
}catch(Exceptione){
System.err.println("客戶端:"+client.getInetAddress().getLocalHost().getHostAddress()+"已斷開連接!");
}
}
}
}
//客戶端程序:
importjava.io.*;
importjava.net.Socket;

{
publicstaticvoidmain(String[]args)throwsIOException{
newTCPClient().init();
}
privatevoidinit()throwsIOException{
@SuppressWarnings("resource")
finalSocketclient=newSocket("127.0.0.1",1000);
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));
Stringsend="";
while(true){
send=in.readLine();
PrintWriterout=newPrintWriter(client.getOutputStream(),true);
if(!"byte".equals(send.trim()))
out.println(send);
else{
out.println(send);
System.exit(0);
}
newThread(newTCPClient(){
@SuppressWarnings("static-access")
publicvoidrun(){
try{
BufferedInputStreambis=newBufferedInputStream(client.getInputStream());
byte[]b=newbyte[1024];
intlen=0;
while((len=bis.read(b))!=-1){
System.out.println("伺服器:"+client.getInetAddress().getLocalHost().getHostAddress()+"發來消息:"+newString(b,0,len).trim());
}
}catch(IOExceptione){
System.err.println("連接伺服器失敗!");
}
}
}).start();
}
}
publicvoidrun(){}
}

//伺服器測試結果:

客戶端:192.168.0.200發來消息:001 byte

客戶端:192.168.0.200發來消息:byte

客戶端:192.168.0.200 已斷開連接!

客戶端:192.168.0.200發來消息:adasd

客戶端:192.168.0.200 已斷開連接!

//客戶端測試結果:

---001號客戶端--

001byte

伺服器:192.168.0.200發來消息:001byte

byte //001禮貌說跟伺服器說byte

---002號客戶端--

adasd //002客戶端直接關閉程序

伺服器:192.168.0.200發來消息:adasd

❹ java socket如何實現客戶端與客戶端的交互

問題一:客復戶端接收可以採制用下列步驟:
1、無論客戶端還是伺服器都要有能力構造實體bean(比如叫做userbean,存放用戶信息),構造userlist封裝userbean數組。並且,上述類要支持序列化和反序列化。
2、服務端將list
userlist序列化,然後利用serversocket發送。
3、客戶端利用socket接收,對userlist反序列化,遍歷userbean數組得到每一個用戶的信息。
問題二:對於socket,不存在得到還是得不到的——
你要利用tcp、udp協議先要構造和初始化socket才行。
客戶端socket和服務端socket進行全雙工通信。
即使是最簡單im演示程序,用戶的好友列表都應該考慮在伺服器端持久化和管理。
客戶端程序要想得到其好友列表,只需要執行一次」請求/響應」即可。寫這方面的程序,可以參考xmpp相關技術。

閱讀全文

與javasocket客戶端相關的資料

熱點內容
最右app緩存在哪個文件夾 瀏覽:157
安卓bochs教程 瀏覽:831
地下室外牆計算工具 瀏覽:805
數控銑床鑽5個孔怎麼編程 瀏覽:66
買美白產品去哪個app 瀏覽:543
word文件如何附文件 瀏覽:139
編程貓是哪個省份的 瀏覽:927
開始學編程從什麼開始 瀏覽:703
數據怎麼退 瀏覽:774
iphone6plusqq郵箱 瀏覽:337
小米自帶文件管理找不到文件 瀏覽:40
win7增強工具 瀏覽:733
網路應該實名制嗎 瀏覽:456
tcp工具 瀏覽:343
火影忍者手游升級 瀏覽:562
win10軟體太小 瀏覽:321
電腦無法載入配置文件 瀏覽:846
linux設置自動關機 瀏覽:173
蘋果電腦壓縮音頻文件 瀏覽:57
dat格式文件如何打開軟體 瀏覽:825

友情鏈接