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

java socket发送中文乱码_java Socket接收数据乱码问题「建议收藏」));}问题:1.此出输出的数据与我发送的数据不一致2.如果我用strSql=String.valueOf(buffer,0,nDataLen-1);则输出的是方块3.同样我用另外一个程序测试端口6789的数据,打印出来的也是方块,不知道是什么原因,请各位老大帮帮忙分析一下原因,三叩首了!!![/B]测试程序:importjava.nio.channels.ServerSocketCh…

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

));

}

问题:

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

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif;

}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

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif;

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

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif){

//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

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif;

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

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif){

//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

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif!=-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

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif;

}

}

}//else

[B]

if(key.isReadable()){

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

ByteBuffer buffer=ByteBuffer.allocate(8192);

System.out.println(“read…&quot

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif;

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

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif){

//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

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif;

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.net/156772.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月8日 下午10:16
下一篇 2022年7月8日 下午10:36


相关推荐

  • Intellij IDEA2021.1创建Java web项目(超详细)

    Intellij IDEA2021.1创建Java web项目(超详细)IntellijIDEA2021.1点击next填写项目的名称以及位置,finish右键项目,选择addframeworksupport完成之后,项目结构变成了这样接下来,我们在WEB-INF下创建classes,lib文件夹编辑项目结构将outputpath的路径改成classes文件夹的路径接下来点击dependencies,选择加号,选择jarsordirectories点击后,他会弹出一个文件选择框,这里选择lib文件所在位置,之后按照下面这张图

    2022年8月25日
    13
  • PyCharm和Python区别(关系)是什么呢?

    PyCharm和Python区别(关系)是什么呢?1 PyCharm 和 Python 的下载地址和安装方式完全不同 2 Python 是一种计算机程序设计语言 是一种面向对象的动态类型语言 最初被设计用于编写自动化脚本 shell 随着版本的不断更新和语言新功能的添加 越来越多被用于独立的 大型项目的开发 3 PyCharm 是 Python 的专用 IDE 地位类似于 Java 的 IDEEclipse 功能齐全的集成开发环境同时提供收费版和免费版 即专业版和社区版 PyCharm 是安装最快的 IDE 且安装后的配置也非常简单 因此 PyChar

    2026年3月27日
    2
  • 一文看懂,2026火爆全网的Claude Skills到底是什么?保姆级教程,手把手教AI零基础小白玩转Skills

    一文看懂,2026火爆全网的Claude Skills到底是什么?保姆级教程,手把手教AI零基础小白玩转Skills

    2026年3月15日
    2
  • idea 2021.8.2激活码(注册激活)

    (idea 2021.8.2激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~Z9LZO4ZKWA-eyJsaWNlb…

    2022年3月22日
    147
  • Air系列模块常见问题列表[通俗易懂]

    Air系列模块常见问题列表[通俗易懂]目录名称一、Luatools使用问题1.1烧录下载 1.1.1、2G模块无法烧录下载 1.1.2、2G开发板无法烧录下载 1.1.3、4G模块(开发板)无法烧录下载 1.1.4、生成量产文件时的加密功能有什么用 1.1.5、4G开发模式下的“USB打印Trace、UART1打印Trace、UART2打印Trace”是什么功能 1.1.6、Luat开发方式下可以烧录哪种类型的文件 1.1.7、脚本代码中如何读取通过Luatools烧录进模块的文件 1.1.8、Luat开发方式下可以烧录某

    2022年5月12日
    44
  • Lucene源码解析–TokenStream和AttributeSource

    Lucene源码解析–TokenStream和AttributeSource转 http://blog.itpub.net/28624388/viewspace-765691/一:Lucene的概况<style./*Style.Definitions*/table.MsoNormalTable{mso-style-name:普通表格;mso-tstyle-rowband-size:0;mso-tstyle-colband-size:0;…

    2022年7月22日
    11

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号