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


相关推荐

  • 下拉框Html.DropDownList 和DropDownListFor 的经常用法

    下拉框Html.DropDownList 和DropDownListFor 的经常用法

    2021年12月3日
    47
  • pytest重试_pycharm could not find main

    pytest重试_pycharm could not find main安装:pip3installpytest-rerunfailures重新运行所有失败用例要重新运行所有测试失败的用例,请使用–reruns命令行选项,并指定要运行测试的最大次数:$py

    2022年7月29日
    7
  • 单片机相关知识点,最强科普总结!

    单片机相关知识点,最强科普总结!0 前言 MCU 是 Microcontrol 的简称 中文叫微控制器 俗称单片机 是把 CPU 的频率与规格做适当缩减 并将内存 计数器 USB A D 转换 UART PLC DMA 等周边接口 甚至 LCD 驱动电路都整合在单一芯片上 形成芯片级的计算机 为不同的应用场合做不同组合控制 诸如手机 PC 外围 遥控器 至汽车电子 工业上的步进马达 机器手臂的控制等

    2026年3月26日
    1
  • html的空格代码怎么写?教你如何使用空格nbsp代码(收藏)

    html的空格代码怎么写?教你如何使用空格nbsp代码(收藏)本篇文章为大家介绍的是 HTML 的空格代码的写法 nbsp 代码的用法 还有几种空格方式的解释 都在文章中 现在开始往下看吧 首先 我们知道这 HTML 网页中插入多个空格间隔是需要特殊字符编码的 如果是直接敲入多个空格键的话 虽然看似代码中有了多个空格效果 但其实在浏览器中还是只有 1 个空格间隔位置的 接下来教大家如果输入 html 空格字符的话 多个空格字符是如何输入的 我们采用直接复制空格字符与 DW 软件输入空格字符的两种方法介绍 web 前端全栈资料粉丝福利 面试题 视频 资料笔记 进阶路

    2026年3月17日
    2
  • linux 解压缩zip文件 unzip 命令详解[通俗易懂]

    linux 解压缩zip文件 unzip 命令详解[通俗易懂]linuxunzip命令详解功能说明:解压缩zip文件语法:unzip[-cflptuvz][-agCjLMnoqsVX][-P][.zip文件][文件][-d][-x]或unzip[-Z]补充说明:unzip为.zip压缩文件的解压缩程序。参数:-c将解压缩的结果显示到屏幕上,并对字符做适当的转换。-f更新现有的文件。

    2022年6月5日
    46
  • 安卓和ios之间文件互传_安卓转移到iphone12

    安卓和ios之间文件互传_安卓转移到iphone12如果之前是安卓用户,在购买iphone12新款手机之后,如何从安卓转移数据到ios?可以通过苹果官方提供的“转移到ios”应用,将安卓手机中的内容进行转移,感兴趣的朋友快来看看吧!如何将数据从安卓设备转移到iphone12可转移的内容包括:通讯录、信息历史记录、相机照片和视频、web书签、邮件帐户和日历。转移完成之后,您可以从appstore下载任何匹配的免费app。使用前准备…

    2026年1月20日
    3

发表回复

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

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