当前位置: 技术问答>java相关
Java通讯程序的中文问题?
来源: 互联网 发布时间:2015-02-03
本文导语: 请问如何解决下面程序的中文问题: 发送端程序: import java.io.*; import java.net.*; public class a1807 { static final int port = 9696; public static void main(String args[]) throws Exception { if (args.length!=2) { System.out.p...
请问如何解决下面程序的中文问题:
发送端程序:
import java.io.*;
import java.net.*;
public class a1807
{
static final int port = 9696;
public static void main(String args[]) throws Exception
{
if (args.length!=2)
{
System.out.println("java a1807 ");
System.exit(0);
}
InetAddress address=InetAddress.getByName(args[0]);
int msglen = args[1].length();
byte msg[] = new byte[msglen];
args[1].getBytes(0,msglen,msg,0);
DatagramPacket packet = new DatagramPacket(msg,msglen,address,port);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
}
}
接收端程序:
import java.io.*;
import java.net.*;
public class a1806
{
static final int port=9696;
public static void main(String args[]) throws Exception
{
byte buf[]=new byte[1024];
String str;
for ( ; ; )
{
DatagramPacket packet = new DatagramPacket(buf,buf.length);
DatagramSocket socket = new DatagramSocket(port);
socket.receive(packet);
str = new String(buf,0,0,packet.getLength());
System.out.println("UDP receive form " +
packet.getAddress().getHostName()+"npacket port=" +
packet.getPort() + "nmsg=" + str);
socket.close();
}
}
}
1、开一个DOS窗口,先运行接收端:
java a1806
2、再开一个DOS窗口,运行发送端:
java a1807 127.0.0.1 OK!
java a1807 127.0.0.1 你好!
接收端显示:
UDP receive form 127.0.0.1
packet port=1112
msg=OK!
UDP receive form 127.0.0.1
packet port=1113
msg=`}
请问,如何解决这个程序的中文问题,我已经试过了:
byte[] tmpstr=str.getBytes("ISO-8859-1");
str = new String(tmpstr);
以及其它的几种方法,都不行。这是为什么?
还有,有没有方法可以从发送端就解决编码问题?
发送端程序:
import java.io.*;
import java.net.*;
public class a1807
{
static final int port = 9696;
public static void main(String args[]) throws Exception
{
if (args.length!=2)
{
System.out.println("java a1807 ");
System.exit(0);
}
InetAddress address=InetAddress.getByName(args[0]);
int msglen = args[1].length();
byte msg[] = new byte[msglen];
args[1].getBytes(0,msglen,msg,0);
DatagramPacket packet = new DatagramPacket(msg,msglen,address,port);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
}
}
接收端程序:
import java.io.*;
import java.net.*;
public class a1806
{
static final int port=9696;
public static void main(String args[]) throws Exception
{
byte buf[]=new byte[1024];
String str;
for ( ; ; )
{
DatagramPacket packet = new DatagramPacket(buf,buf.length);
DatagramSocket socket = new DatagramSocket(port);
socket.receive(packet);
str = new String(buf,0,0,packet.getLength());
System.out.println("UDP receive form " +
packet.getAddress().getHostName()+"npacket port=" +
packet.getPort() + "nmsg=" + str);
socket.close();
}
}
}
1、开一个DOS窗口,先运行接收端:
java a1806
2、再开一个DOS窗口,运行发送端:
java a1807 127.0.0.1 OK!
java a1807 127.0.0.1 你好!
接收端显示:
UDP receive form 127.0.0.1
packet port=1112
msg=OK!
UDP receive form 127.0.0.1
packet port=1113
msg=`}
请问,如何解决这个程序的中文问题,我已经试过了:
byte[] tmpstr=str.getBytes("ISO-8859-1");
str = new String(tmpstr);
以及其它的几种方法,都不行。这是为什么?
还有,有没有方法可以从发送端就解决编码问题?
|
这样改改可能可以:
import java.io.*;
import java.net.*;
public class a1807
{
static final int port = 9696;
public static void main(String args[]) throws Exception
{
if (args.length!=2)
{
System.out.println("java a1807 ");
System.exit(0);
}
InetAddress address=InetAddress.getByName(args[0]);
//int msglen = args[1].length();
//byte msg[] = new byte[msglen];
byte msg[] = args[1].getBytes();
//args[1].getBytes(0,msglen,msg,0);
DatagramPacket packet = new DatagramPacket(msg,msg.length,address,port);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
}
}
import java.io.*;
import java.net.*;
public class a1807
{
static final int port = 9696;
public static void main(String args[]) throws Exception
{
if (args.length!=2)
{
System.out.println("java a1807 ");
System.exit(0);
}
InetAddress address=InetAddress.getByName(args[0]);
//int msglen = args[1].length();
//byte msg[] = new byte[msglen];
byte msg[] = args[1].getBytes();
//args[1].getBytes(0,msglen,msg,0);
DatagramPacket packet = new DatagramPacket(msg,msg.length,address,port);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
}
}
|
这样应该可以,我在win2000下通过:
al806.java:
import java.io.*;
import java.net.*;
public class al806
{
static final int port=9696;
public static void main(String args[]) throws Exception
{
byte buf[]=new byte[1024];
String str;
for ( ; ; )
{
DatagramPacket packet = new DatagramPacket(buf,buf.length);
DatagramSocket socket = new DatagramSocket(port);
socket.receive(packet);
str = new String(packet.getData(),0,packet.getLength());
System.out.println("UDP receive form " +
packet.getAddress()+"npacket port=" +
packet.getPort() + "nmsg=" + str);
socket.close();
}
}
}
al807.java:
import java.io.*;
import java.net.*;
public class al807
{
static final int port = 9696;
public static void main(String args[]) throws Exception
{
if (args.length!=2)
{
System.out.println("java a1807 ");
System.exit(0);
}
InetAddress address=InetAddress.getByName(args[0]);
byte[] msg=args[1].getBytes();
DatagramPacket packet = new DatagramPacket(msg,msg.length,address,port);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
}
}
试试吧!
al806.java:
import java.io.*;
import java.net.*;
public class al806
{
static final int port=9696;
public static void main(String args[]) throws Exception
{
byte buf[]=new byte[1024];
String str;
for ( ; ; )
{
DatagramPacket packet = new DatagramPacket(buf,buf.length);
DatagramSocket socket = new DatagramSocket(port);
socket.receive(packet);
str = new String(packet.getData(),0,packet.getLength());
System.out.println("UDP receive form " +
packet.getAddress()+"npacket port=" +
packet.getPort() + "nmsg=" + str);
socket.close();
}
}
}
al807.java:
import java.io.*;
import java.net.*;
public class al807
{
static final int port = 9696;
public static void main(String args[]) throws Exception
{
if (args.length!=2)
{
System.out.println("java a1807 ");
System.exit(0);
}
InetAddress address=InetAddress.getByName(args[0]);
byte[] msg=args[1].getBytes();
DatagramPacket packet = new DatagramPacket(msg,msg.length,address,port);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
}
}
试试吧!