導航:首頁 > 編程語言 > javasocket字元串

javasocket字元串

發布時間:2023-01-01 01:53:09

1. java socket發送字元串

java中socket是向某個特定地址的埠發送流(字元串通過getBytes方法轉換成流)。

publicstaticvoidmain(String[]args){
Sockets;
try{
s=newSocket("localhost",9091);

DataOutputStreamout=newDataOutputStream(s.getOutputStream());
out.write("aslkjlksjdsad".getBytes());
out.flush();
out.close();
}catch(UnknownHostExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}

備註:有發送的話,就需要相應的地址和埠上做一個接收的,來實現交互。

2. 關羽java的socket傳輸字元串數組問題

給你一段代碼,不知道能不能解決你的問題
可以直接傳字元串,也可以將字元串數組封裝對象進行傳輸。
服務端代碼:
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer {
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(9091);
Socket s = ss.accept();
System.out.println("new connection");
ObjectInputStream oi = new ObjectInputStream(s.getInputStream());
String [] arr = (String[]) oi.readObject();
System.out.println(arr[0] + arr[1]);
}
}
客戶端代碼:
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Mysocket {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost",9091);
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(new String[]{"123","232"});
out.flush();
out.close();
}
}

3. java接受c語言socket字元串

兩邊要對應的,才行。如果C也是按UTF8發送的,接收用readUTF()才行。
否則,就用一般的讀取函數

4. java-socket 如何判斷網路傳過來的是字元串還是對象

socket建立連接的getInputstream()和getOutputStream()方法都是位元組流。
可以通過writeUTF等方法來進行寫入。
如果想判斷是字元串還是對象,你可以對對象進行一下處理。這個對象是某個類的實例,這個類裡面定義一個標識屬性,比如public int flag,通過位元組流進行交流後,這邊讀取到,首先讀取一個int數據,如果是1或者是你令的別的標志,那麼就是對象,按對象處理。如果readInt後不能轉化為你要求的那個標識,那麼就是字元串。用public boolean flag等也可。
都是位元組流過來的,讀取規定位元組數看看是不是你事先定好的標志。

5. 在java Socket編程中(client給server發送一個字元串,再由server返回一個字元串給client)

SERVER端:
--------------------------------------------------------
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server extends Thread {

private Socket clientSocket;

public Server(Socket clientSocket) {
this.clientSocket = clientSocket;
}

public void run() {

DataInputStream dis = null;
DataOutputStream dos = null;

try {

dis = new DataInputStream(clientSocket.getInputStream());
dos = new DataOutputStream(clientSocket.getOutputStream());

while (true) {
String temp = dis.readUTF();
if ("over".equals(temp)) {
break;
}
dos.writeUTF("from server:" + temp);
}

} catch (Exception e) {
//e.printStackTrace();
} finally {
try {
if (dis != null) {
dis.close();
}
if (dis != null) {
dos.close();
}

if (clientSocket != null) {
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
System.out.println("Server Thread is shutdown.");
}
}
}

public static void main(String[] args) throws Exception {

ServerSocket ss = new ServerSocket(8008);
while (true) {
Socket clientSocket = ss.accept();
// 針對每個客戶端, 啟一個Server線程專門處理此客戶端的請求。
Server server = new Server(clientSocket);
server.start();
}

}

}

CLIENT端:
----------------------------------------
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class Client {

public static void main(String[] args) throws Exception {

// 輸入流1, 從鍵盤進入Client。
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

Socket clientSocket = new Socket("127.0.0.1", 8008);

// 輸入流2, 從伺服器端進入Client的流對象。
DataInputStream dis = new DataInputStream(clientSocket.getInputStream());

// 輸出流, 從Client出去, 到伺服器端。
DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());

while (true) {

// 從鍵盤輸入讀取
String msg = br.readLine();

// 將讀取信息發送給伺服器端
dos.writeUTF(msg);

//輸入over退出
if ("over".equals(msg)) {
break;
}

//讀取從伺服器返回的信息
String temp = dis.readUTF();
System.out.println(temp);
}

br.close();
dis.close();
dos.close();
clientSocket.close();

}

}
--------------------------------------------------------

閱讀全文

與javasocket字元串相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:962
phpffmpeg轉碼 瀏覽:671
長沙好玩的解壓項目 瀏覽:144
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:736
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:484
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:381
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:349
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:892
app轉賬是什麼 瀏覽:163