前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java socket发送中文乱码_java Socket接收数据乱码问题「建议收藏」

java socket发送中文乱码_java Socket接收数据乱码问题「建议收藏」

作者头像
全栈程序员站长
发布2022-09-08 12:16:26
1.5K0
发布2022-09-08 12:16:26
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

));

}

问题:

1.此出输出的数据与我发送的数据不一致

2.如果我用strSql = String.valueOf(buffer,0,nDataLen – 1 );则输出的是方块

3.同样我用另外一个程序测试端口6789的数据,打印出来的也是方块,不知道是什么原因,请各位老大帮帮忙分析一下原因,三叩首了!!!

[/B]

测试程序:

import java.nio.channels.ServerSocketChannel;

import java.nio.channels.SocketChannel;

import java.net.ServerSocket;

import java.net.InetSocketAddress;

import java.nio.channels.Selector;

import java.nio.channels.SelectionKey;

import java.io.IOException;

import java.util.Iterator;

import java.nio.ByteBuffer;

import java.util.ArrayList;

import java.nio.charset.*;

import java.nio.*;

public class ChatServer {

private int port = 6789;

private Selector selector;

private ServerSocketChannel ssc;

private ServerSocket server;

private InetSocketAddress address;

private ArrayList connectKey=new ArrayList();

public ChatServer(){

//initServer

try{

ssc=ServerSocketChannel.open();

server=ssc.socket();

address = new InetSocketAddress(port);

server.bind(address);

selector=Selector.open();

ssc.configureBlocking(false);

ssc.register(selector,SelectionKey.OP_ACCEPT);

System.out.println(“Listening the port 6789…&quot

;

}catch(IOException ex){

ex.printStackTrace();

System.exit(-1);

}

}

public void startServer() throws IOException{

while(true){

int i=selector.select();

//System.out.print(i);

Iterator keys = selector.selectedKeys().iterator();

while(keys.hasNext()){

SelectionKey key = (SelectionKey)keys.next();

keys.remove();

try{

if(key.isAcceptable()){

ServerSocketChannel ssc=(ServerSocketChannel)key.channel();

SocketChannel channel = ssc.accept();//return null if there’s no request

System.out.println(channel+” has accepted&quot

;

channel.configureBlocking(false);

SelectionKey clientKey=channel.register(selector,SelectionKey.OP_READ);

}//else

if(key.isWritable()){

SocketChannel channel = (SocketChannel)key.channel();

ByteBuffer buffer = (ByteBuffer)key.attachment();

if(buffer!=null){

key.attach(null);//avoid the return twice

//check info:the login or the message

//buffer.flip();

String checkBuffer = this.decode(buffer);

System.out.println(“write:”+checkBuffer);

if(checkBuffer.equals(“LOGIN:OK&quot

){

//return LOGIN:OK then add into the connectKey array!

System.out.println(“ok”+buffer);

buffer.flip();

//while(buffer.hasRemaining()&channel.write(buffer)!=-1);

channel.write(buffer);

key.interestOps(SelectionKey.OP_READ|SelectionKey.OP_WRITE);

connectKey.add(key);//add to the connectKey array

System.out.println(“here&quot

;

}else if(checkBuffer.equals(“LOGIN:ERROR&quot

){

//return LOGIN:ERROR the client should close the channel

//warning:method:key.channel();

//Returns the channel for which this key was created.

// This method will continue to return the channel even after the key is cancelled.

while(buffer.hasRemaining()&channel.write(buffer)!=-1);

key.cancel();

}else //if(checkBuffer.indexOf(“SENTO:&quot

!=-1){

{

//return the message to everyone

// while(buffer.hasRemaining()&channel.write(buffer)!=-1);

System.out.println(“sento”+buffer);

buffer.flip();

channel.write(buffer);

System.out.println(“send over&quot

;

}

}

}//else

[B]

if(key.isReadable()){

SocketChannel channel = (SocketChannel)key.channel();

ByteBuffer buffer=ByteBuffer.allocate(8192);

System.out.println(“read…&quot

;

channel.read(buffer);

buffer.flip();

String checkBuffer = this.decode(buffer);

System.out.println(“read:”+checkBuffer);

[/B]

//while(buffer.hasRemaining()&&channel.read(buffer)!=-1);

//check the buffer

//buffer.flip();

//String checkBuffer = this.decode(buffer);

// System.out.println(“read:”+checkBuffer);

if(checkBuffer.startsWith(“LOGIN:&quot

){

//get info of the user & pass then check for it,return feedback!

//the format is LOGIN:user&pass

int p1=checkBuffer.length();

int p2=checkBuffer.indexOf(“&&quot

;

String user=checkBuffer.substring(6,p2);

String pass=checkBuffer.substring(p2+1,p1);

System.out.println(user+pass);

//todo check from the database!!!

//assume the user is legal

ByteBuffer feedback = ByteBuffer.allocate(20);

feedback=ByteBuffer.wrap(“LOGIN:OK”.getBytes());

key.interestOps(SelectionKey.OP_WRITE);

key.attach(feedback);

}else if(checkBuffer.startsWith(“SENTO:”)){

String message = checkBuffer.substring(6);

System.out.println(“sentto:”+message);

ByteBuffer buffer1 = ByteBuffer.allocate(50);

buffer1=ByteBuffer.wrap(message.getBytes());

Iterator it = connectKey.iterator();

//key.interestOps(SelectionKey.OP_WRITE);

while(it.hasNext()){

((SelectionKey)it.next()).attach(buffer1.duplicate());

}

System.out.println(“here1”);

//for(int i=0;i //connectKey.attach(buffer.duplicate());

//}

}

}

}catch(IOException ex){

key.cancel();

//System.exit(-1);

try{

key.channel().close();

}catch(IOException cex){

}

}

}

}

}

public String decode(ByteBuffer buffer){

Charset charset=null;

CharsetDecoder decoder=null;

CharBuffer charBuffer=null;

try{

charset= Charset.forName(“ISO-8859-1”);

decoder= charset.newDecoder();

charBuffer= decoder.decode(buffer);

return charBuffer.toString();

}catch(Exception ex){

ex.printStackTrace();

return “”;

}

}

public static void main(String []args){

ChatServer cs = new ChatServer();

try{

cs.startServer();

}catch(IOException ex){

ex.printStackTrace();

System.exit(-1);

}

}

}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/156772.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档