当前位置: 技术问答>java相关
请问如何能在网络传送中文信息??
来源: 互联网 发布时间:2015-05-06
本文导语: 我正在用java做一个网络程序,形式如QQ一样,但传送中文信息时,不能正确显示!请问各位大虾们如何解决? | 以下经调试可行: static void sendData(){ try { ...
我正在用java做一个网络程序,形式如QQ一样,但传送中文信息时,不能正确显示!请问各位大虾们如何解决?
|
以下经调试可行:
static void sendData(){
try
{
String msg=("testit中午");//textField1.getText ();
if(msg.equals (""))
return;
// textField1.setText ("");
InetAddress address=InetAddress.getByName ("10.10.1.37");
// int len=msg.length ();
// byte[] message=new byte[len];
// msg.getBytes(0,len,message,0);
byte[] message=msg.getBytes();
DatagramPacket packet =new DatagramPacket (message,message.length,address,9999);
DatagramSocket socket=new DatagramSocket ();
socket.send (packet);
}
catch(Exception e)
{
e.printStackTrace();
}
}
static void waitForData()
{
try{
byte[] buffer=new byte[1024];
DatagramPacket packet=new DatagramPacket (buffer,buffer.length );
DatagramSocket socket=new DatagramSocket (9999);
while(true)
{
socket.receive(packet);
// String s=new String(buffer,0,0,packet.getLength ());
String s=new String(buffer,0,packet.getLength());
System.out.println("getData:"+(s));
packet=new DatagramPacket (buffer,buffer.length);
}
}
catch(Exception e)
{
}
}
static void sendData(){
try
{
String msg=("testit中午");//textField1.getText ();
if(msg.equals (""))
return;
// textField1.setText ("");
InetAddress address=InetAddress.getByName ("10.10.1.37");
// int len=msg.length ();
// byte[] message=new byte[len];
// msg.getBytes(0,len,message,0);
byte[] message=msg.getBytes();
DatagramPacket packet =new DatagramPacket (message,message.length,address,9999);
DatagramSocket socket=new DatagramSocket ();
socket.send (packet);
}
catch(Exception e)
{
e.printStackTrace();
}
}
static void waitForData()
{
try{
byte[] buffer=new byte[1024];
DatagramPacket packet=new DatagramPacket (buffer,buffer.length );
DatagramSocket socket=new DatagramSocket (9999);
while(true)
{
socket.receive(packet);
// String s=new String(buffer,0,0,packet.getLength ());
String s=new String(buffer,0,packet.getLength());
System.out.println("getData:"+(s));
packet=new DatagramPacket (buffer,buffer.length);
}
}
catch(Exception e)
{
}
}
|
用这个吧。
public static int StringTobytes(String pStrSource,byte[] pbArDest,int piStartPos)
{
if(piStartPos
public static int StringTobytes(String pStrSource,byte[] pbArDest,int piStartPos)
{
if(piStartPos