udp练习,为什么我发送的是汉字,一接受就乱码了呢?数字 英文则没事
1
hahha
站
齐天大圣孙悟空
1
hahha
绔?
榻澶+ぇ?????绌?
4 个解决方案
#1
把你发送数据接收数据的代码发一下
#2
统一编码。。
#3
#4
package _2_system.in;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class send {
public static void main(String[] args) throws IOException {
DatagramSocket s = new DatagramSocket();
// BufferedInputStream br = new BufferedInputStream(System.in);
BufferedReader bw = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = bw.readLine()) != null) {
if ("886".equals(line)) {
break;
}
byte[] bys = line.getBytes();
int len = bys.length;
DatagramPacket dp = new DatagramPacket(bys, len,
InetAddress.getByName("192.168.1.121"), 10086);
s.send(dp);
}
s.close();
}
}
package _2_system.in;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class sever {
public static void main(String[] args) throws IOException {
DatagramSocket s = new DatagramSocket(10086);
while (true) {
byte[] bys = new byte[1024];
int len = bys.length;
DatagramPacket dp = new DatagramPacket(bys, len);
s.receive(dp);
byte[] byc = dp.getData();
int len2 = dp.getLength();
String ss = new String(byc, 0, len2);
System.out.println(ss);
InetAddress ip = dp.getAddress();
String ipa = ip.getHostAddress();
String ipn = ip.getHostName();
System.out.println(ipa + ipn);
}
}
}
谢谢
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class send {
public static void main(String[] args) throws IOException {
DatagramSocket s = new DatagramSocket();
// BufferedInputStream br = new BufferedInputStream(System.in);
BufferedReader bw = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = bw.readLine()) != null) {
if ("886".equals(line)) {
break;
}
byte[] bys = line.getBytes();
int len = bys.length;
DatagramPacket dp = new DatagramPacket(bys, len,
InetAddress.getByName("192.168.1.121"), 10086);
s.send(dp);
}
s.close();
}
}
package _2_system.in;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class sever {
public static void main(String[] args) throws IOException {
DatagramSocket s = new DatagramSocket(10086);
while (true) {
byte[] bys = new byte[1024];
int len = bys.length;
DatagramPacket dp = new DatagramPacket(bys, len);
s.receive(dp);
byte[] byc = dp.getData();
int len2 = dp.getLength();
String ss = new String(byc, 0, len2);
System.out.println(ss);
InetAddress ip = dp.getAddress();
String ipa = ip.getHostAddress();
String ipn = ip.getHostName();
System.out.println(ipa + ipn);
}
}
}
谢谢
#1
把你发送数据接收数据的代码发一下
#2
统一编码。。
#3
#4
package _2_system.in;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class send {
public static void main(String[] args) throws IOException {
DatagramSocket s = new DatagramSocket();
// BufferedInputStream br = new BufferedInputStream(System.in);
BufferedReader bw = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = bw.readLine()) != null) {
if ("886".equals(line)) {
break;
}
byte[] bys = line.getBytes();
int len = bys.length;
DatagramPacket dp = new DatagramPacket(bys, len,
InetAddress.getByName("192.168.1.121"), 10086);
s.send(dp);
}
s.close();
}
}
package _2_system.in;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class sever {
public static void main(String[] args) throws IOException {
DatagramSocket s = new DatagramSocket(10086);
while (true) {
byte[] bys = new byte[1024];
int len = bys.length;
DatagramPacket dp = new DatagramPacket(bys, len);
s.receive(dp);
byte[] byc = dp.getData();
int len2 = dp.getLength();
String ss = new String(byc, 0, len2);
System.out.println(ss);
InetAddress ip = dp.getAddress();
String ipa = ip.getHostAddress();
String ipn = ip.getHostName();
System.out.println(ipa + ipn);
}
}
}
谢谢
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class send {
public static void main(String[] args) throws IOException {
DatagramSocket s = new DatagramSocket();
// BufferedInputStream br = new BufferedInputStream(System.in);
BufferedReader bw = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = bw.readLine()) != null) {
if ("886".equals(line)) {
break;
}
byte[] bys = line.getBytes();
int len = bys.length;
DatagramPacket dp = new DatagramPacket(bys, len,
InetAddress.getByName("192.168.1.121"), 10086);
s.send(dp);
}
s.close();
}
}
package _2_system.in;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class sever {
public static void main(String[] args) throws IOException {
DatagramSocket s = new DatagramSocket(10086);
while (true) {
byte[] bys = new byte[1024];
int len = bys.length;
DatagramPacket dp = new DatagramPacket(bys, len);
s.receive(dp);
byte[] byc = dp.getData();
int len2 = dp.getLength();
String ss = new String(byc, 0, len2);
System.out.println(ss);
InetAddress ip = dp.getAddress();
String ipa = ip.getHostAddress();
String ipn = ip.getHostName();
System.out.println(ipa + ipn);
}
}
}
谢谢