服务器端
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();
}
}
解决方案 »
- 求JAVA程序设计课后答案
- 弱弱的问个菜鸟的继承问题??
- windows编程和JAVA编程哪个更该先学?
- RSS 2.0规范(中文版)
- 八位二进制如何表示负90
- 请问我将调用JNI的类放到一个包里面就出错,如果没有包就可以,什么原因?怎么解决???
- 有关java和数据库的小问题
- 如何在JPanel当中画出一个图像?
- 新手求教!2个字符串如何从字符串的第1个字符开始匹配?
- java.lang.IllegalArgumentException: Invalid character found in method name. HTTP
- 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都写的话,应该没问题的。
另外,如果一个端点是日文系统,另一端是中文系统,用你的方法可以正确显示吗? 迷惑中。。