服务器端
package test.socket;import java.net.ServerSocket;
import java.net.Socket;
import java.lang.Thread;
import java.io.InputStream;public class Server implements Runnable
{
final static private int port = 9999;
private Socket aSocket; public static final void main( String[] argv ) throws Exception
{
ServerSocket aServerSocket = new ServerSocket( port ); System.out.println( "Server run on port: " + port );
while( true )
{
try
{
Socket aSocket = aServerSocket.accept();
System.out.println( "receive a connect" ); Server aServer = new Server( aSocket );
Thread aThread = new Thread( aServer );
aThread.start();
}
catch( Exception e )
{
}
}
} public Server( Socket aSocket )
{
this.aSocket = aSocket;
} public void run()
{
try
{
InputStream is = aSocket.getInputStream(); while( true )
{
try
{
int length = is.available();
byte[] bytes = new byte[length];
is.read( bytes );
System.out.print( new String( bytes, "gb2312" ) );
}
catch( Exception e )
{
}
}
}
catch( Exception e )
{
}
}
}客户端
package test.socket;import java.net.Socket;
import java.io.OutputStreamWriter;public class Client
{
final static private int port = 9999; public static final void main( String[] argv ) throws Exception
{
char aChar;
StringBuffer aLine = new StringBuffer(); System.out.println( "connect to port " + port );
System.out.println( "send string \"汉字测试 and abc\"" );
Socket aSocket = new Socket( "127.0.0.1", port );
OutputStreamWriter osw = new OutputStreamWriter( aSocket.getOutputStream(), "gb2312" ); osw.write( "汉字测试 and abc\n" );
osw.flush();
aSocket.close();
} protected static void printBytes( String testStr, String encoding ) throws Exception
{
byte[] bs;
if( encoding != null )
bs = testStr.getBytes( encoding );
else
bs = testStr.getBytes();
System.out.println( "字串是: " + testStr );
if( encoding != null )
System.out.println( "encoding: " + encoding );
else
System.out.println( "encoding: 默认" ); System.out.print( "Bytes[" + bs.length + "]: " );
for( int i = 0; i < bs.length; i++ )
{
int value = ( int )bs[i]; if( value < 0 )
value += 256;
System.out.print( value + " " );
}
System.out.println();
System.out.println( "decode: " + new String( bs ) );
if( encoding != null )
System.out.println( "another decode: " + new String( testStr.getBytes(), encoding ) );
System.out.println();
}
}

解决方案 »

  1.   

    sorry,这个客户端最后的一个函数没用,是我原来用的,忘了删了
      

  2.   

    吧汉字转化成byte[]传送,在接受时转化成String就可以了,我就是这样做的
      

  3.   

    不用那么麻烦,
    用DataOutputStream.writeUTF发送汉字,
    用DataInputStream.readUTF接收汉字。
      

  4.   

    to farawayzheng_necas(遥远):要使用你这么做的方法的话,如果对方的客户端使用telnet连接怎么办?不是所有的东西发汉字都使用unicode的
      

  5.   

    nod,转成byte我也是这么用的,没问题
      

  6.   

    OutputStreamWriter osw = new OutputStreamWriter( aSocket.getOutputStream(), "gb2312" );
    osw.write( "汉字测试 and abc\n" );
    这段代码换成
    OutputStream osw = aSocket.getOutputStream();
    byte[] bts="汉字测试 and abc\n".getBytes("gb2312");
    osw.write( bts );
    试试看.
      

  7.   

    to SAsura(SAsura)
    那就要看楼主用socket做什么了,如果server和client都写的话,应该没问题的。
    另外,如果一个端点是日文系统,另一端是中文系统,用你的方法可以正确显示吗? 迷惑中。。