服务器端
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();
}
}
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();
}
}
解决方案 »
- 局部变量声明一个问题
- 一些复杂的逻辑该放到service里还是放到对象里?
- JBuilder2007的问题!!
- 帮忙看个问题,为什么在程序中import 了java.util后还是报找不到Date这个类~~
- [Swing]JTextPane的Document里插如超链接事件
- 请高手帮我看一下这段代码到底有什么问题?
- 用按钮怎样把JList里面的一项删除?
- dom4j各个版本中使用的jdk的版本?
- bool值:10 > 5 && 1 > 0 || 3 > 5 || 20 == 8
- SCJD是什么东西,在线等待。
- English---Japanese and other languages in the world??
- 关于java程序员的讨论?
用DataOutputStream.writeUTF发送汉字,
用DataInputStream.readUTF接收汉字。
osw.write( "汉字测试 and abc\n" );
这段代码换成
OutputStream osw = aSocket.getOutputStream();
byte[] bts="汉字测试 and abc\n".getBytes("gb2312");
osw.write( bts );
试试看.
那就要看楼主用socket做什么了,如果server和client都写的话,应该没问题的。
另外,如果一个端点是日文系统,另一端是中文系统,用你的方法可以正确显示吗? 迷惑中。。