導航:首頁 > 編程語言 > 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字元串相關的資料

熱點內容
android表格demo 瀏覽:260
安卓怎麼讓相冊不顯示網頁 瀏覽:297
php文件系統源碼 瀏覽:710
易信java 瀏覽:483
北侖二級壓縮螺桿機 瀏覽:46
加密路線免費入口 瀏覽:741
計算器程序員是幹嘛的 瀏覽:362
數控編程里fo是啥鍵 瀏覽:657
javacdataxml 瀏覽:945
本機tcp伺服器地址 瀏覽:429
切換pdf 瀏覽:94
iturns下載的app在哪裡 瀏覽:643
雲起書院App在哪裡 瀏覽:617
怎麼對hex文件進行加密 瀏覽:288
pdf少年時 瀏覽:956
兩個同名文件夾同步 瀏覽:632
拍照文件夾哪個好用 瀏覽:93
文件夾和書包 瀏覽:513
解壓歸檔拿登記證 瀏覽:227
去體驗解壓房 瀏覽:615