海詩(shī)網(wǎng) 知識(shí)百科 java(java編程入門到精通)

java(java編程入門到精通)

java, 如何解決java socket中文亂碼?不知道小伙伴們今天來(lái)看看邊肖的分享吧!

您可以使用代碼:

in=new BufferedReader(new InputStreamReader(socket.getInputStream(),UTF-8));

out=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),UTF-8));

java編程入門到精通

示例:

求解前:

import java.io.*;

import java.net.*;

public class SocketServerEx1 {

public static void main(String args[]) {

System.out.println(Server);

ServerSocket serverSocket=null;

Socket clientSocket=null;

int connects=0;

try {

//最多連接10次。

while(connects 10) {

connects++;

System.out.println( -等待連接- );

客戶端套接字=服務(wù)器套接字。接受();//等待連接

System.out.println (the first connection and the second connection);

ServiceClient(clientSocket);

}

serverSocket.close();

} catch(IOException ioe) {

System.out.println(Error: + ioe);

}}

public static void ServiceClient(Socket client) throws IOException {

System.out.println(已鏈接);

InputStreamReader inSR=null;

OutputStreamWriter outSW=null;

try {

//讀取數(shù)據(jù)

inSR=new InputStreamReader(client.getInputStream(), UTF-8);

BufferedReader br=new BufferedReader(inSR);

outSW=new OutputStreamWriter(client.getOutputStream(), UTF-8);

BufferedWriter bw=new BufferedWriter(outSW);

String str=;

while((str=br.readLine()) !=null) {

str=str.trim();

System.out.println(接收的客戶端消息:str);

Bw.write(收到的消息:str/r/n);//將消息反饋給客戶端,添加一個(gè)換行符讓客戶端接收。

bw.flush();

}

} finally {

//System.out.println(Cleaning up connection: + client);

inSR.close();

outSW.close();

client.close();

}

System.out.println(已斷開(kāi));

}

}

java編程入門到精通

解決后:

import java.io.*;

import java.net.*;

public class SocketClientEx1 {

public static void main(String[] args) {

System.out.println(Client);

try {

Socket clientSocket=new Socket(localhost, 82);

System.out.println(Client1: + clientSocket);

DataInputStream dataIS=new DataInputStream(clientSocket.getInputStream());

InputStreamReader inSR=new InputStreamReader(dataIS, UTF-8);

BufferedReader br=new BufferedReader(inSR);

DataOutputStream dataOS=new DataOutputStream(clientSocket.getOutputStream());

BufferedWriter bw=new BufferedWriter(outSW);

//輸入信息

byte bytes[]=new byte[100];

while(true) {

System.out.println(----------------------------------);

System.in.read(bytes);

String str=new String(bytes);

str=str.trim();

if (str==exit) {

break;

}

//發(fā)送數(shù)據(jù)

bw . write(str/r/n);//添加一個(gè)換行符,以便服務(wù)器逐行讀取。

bw.flush();

//接收數(shù)據(jù)

while((str=br.readLine()) !=null) {

str=str.trim();

System.out.println(服務(wù)器回復(fù):str);

break;

}

}

inSR.close();

dataIS.close();

dataOS.close();

clientSocket.close();

} catch(UnknownHostException uhe) {

System.out.println(Error: + uhe.getMessage());

} catch(ConnectException ce) {

System.out.println(Error: + ce.getMessage());

} catch(IOException ioe) {

System.out.println(Error: + ioe.getMessage());

} finally {

}

}

}

java,以上就是本文為您收集整理的java最新內(nèi)容,希望能幫到您!更多相關(guān)內(nèi)容歡迎關(guān)注。

本文來(lái)自網(wǎng)絡(luò),不代表海詩(shī)網(wǎng)立場(chǎng),轉(zhuǎn)載請(qǐng)注明出處:http://x91880.com/n/169092.html
      

msvcp140.dll是什么東西(丟失msvcp140.dll是什么意思)

發(fā)表回復(fù)
聯(lián)系我們
聯(lián)系我們

在線咨詢: QQ交談

郵箱: 3587015498@qq.com

工作時(shí)間:周一至周五,9:00-17:30,節(jié)假日休息

關(guān)注微信
微信掃一掃關(guān)注我們
微信掃一掃關(guān)注我們
關(guān)注微博
返回頂部