關(guān)于socket發(fā)送數(shù)據(jù)需要注意的問(wèn)題
socket發(fā)送數(shù)據(jù)需要注意的問(wèn)題
這兩天在寫socket通信的時(shí)候遇到一個(gè)問(wèn)題,在服務(wù)器端使用BufferedWriter.write()和BufferedWriter.flush()向客戶端發(fā)送信息后,客戶端使用BufferedReader.readLine()怎么也讀不到數(shù)據(jù)。
后來(lái)發(fā)現(xiàn)是因?yàn)闆](méi)有在write之后創(chuàng)建新的行,所以客戶端的readLine()不能判斷行結(jié)束,一直在等待服務(wù)器發(fā)送行結(jié)束符,就出現(xiàn)客戶端接收不到數(shù)據(jù)的現(xiàn)象。
解決的方法
是在write()后newLine(),再flush()客戶端就可以了。
ServerSocket server = null;
try {
server = new ServerSocket(AppProvider.PORT, 0);
BufferedReader in;
BufferedWriter out;
while (true) {
Socket client = server.accept();
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
out.flush();
try {
String request = in.readLine();
String response = processMessage(request);
if (response != null) {
out.write(response);
out.newLine();
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
in.close();
out.close();
client.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (server != null && !server.isClosed()) {
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}Java-socket上傳 / 接收文件
Socket上傳文件
//測(cè)試示例
public void upSocket(){
//Java中使用Socket進(jìn)行文件上傳的基本流程如下所示:
//創(chuàng)建客戶端Socket對(duì)象并連接到服務(wù)器??梢酝ㄟ^(guò)指定IP地址和端口號(hào)來(lái)完成連接操作。
String serverIp = "服務(wù)器IP"; // 設(shè)置服務(wù)器IP地址
int port = 8080; // 設(shè)置服務(wù)器監(jiān)聽(tīng)的端口號(hào)
Socket clientSocket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
clientSocket = new Socket(serverIp, port);
//獲取輸入/輸出流對(duì)象,分別用于讀取要發(fā)送的文件內(nèi)容和寫入從服務(wù)器返回的數(shù)據(jù)。
String filePath = "要上傳的文件路徑";
File fileToSend = new File(filePath);
if (!fileToSend.exists()) {
throw new IllegalArgumentException("指定的文件不存在!");
}
inputStream = new FileInputStream(filePath); // 設(shè)置要上傳的文件路徑
outputStream = clientSocket.getOutputStream();
//將文件內(nèi)容逐段讀取后寫入輸出流,直到文件結(jié)尾。
byte[] buffer = new byte[4096]; // 設(shè)置每次讀取的字節(jié)大小
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush(); // 確保數(shù)據(jù)被全部寫入輸出流
} catch (IOException e) {
e.printStackTrace();
}finally {
//關(guān)閉輸入/輸出流和Socket連接。
try {
inputStream.close();
outputStream.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Socket接收文件
public static void main(String[] args) throws IOException {
int portNumber = 8080;
ServerSocket serverSocket = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
serverSocket = new ServerSocket(portNumber);
System.out.println("正在監(jiān)聽(tīng)端口" + portNumber + "...");
Socket clientSocket = serverSocket.accept();
System.out.println("客戶端已連接!");
inputStream = clientSocket.getInputStream();
fileOutputStream = new FileOutputStream("D:\\test\\received_file.txt");
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
fileOutputStream.close();
inputStream.close();
serverSocket.close();
serverSocket.close();
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot編譯target目錄下沒(méi)有resource下的文件踩坑記錄
這篇文章主要介紹了SpringBoot編譯target目錄下沒(méi)有resource下的文件踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
java判斷今天,昨天,前天,不能用秒間隔的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇java判斷今天,昨天,前天,不能用秒間隔的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
一口氣說(shuō)出Java 6種延時(shí)隊(duì)列的實(shí)現(xiàn)方法(面試官也得服)
這篇文章主要介紹了一口氣說(shuō)出Java 6種延時(shí)隊(duì)列的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
基于Redisson實(shí)現(xiàn)注解式分布式鎖的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何基于Redisson實(shí)現(xiàn)注解式分布式鎖,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以了解一下2023-07-07

