gpt4 book ai didi

Android 客户端,PC 服务器套接字不能超过 3g

转载 作者:行者123 更新时间:2023-11-29 00:45:10 25 4
gpt4 key购买 nike

我尝试在我的机器人(客户端)和我的 PC(服务器)之间创建一个客户端服务器套接字,当我在本地(通过 wifi)时它工作得很好,但是当我尝试超过 3G 时我得到这个异常当服务器尝试获取 clientsocket.getOutputStream()

at java.lang.Thread.run(Unknown Source)
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)

这是什么问题,请问有人知道解决方法吗?请帮助:-(

服务器

public class Server {

ServerSocket serverSocket;
public LinkedBlockingQueue<CDRecCourseDisplay> recCours;
public LinkedList<ClientMail> clientMails;
static Server server;

public static Server getInstance(){
if(server == null){
server = new Server();
}
return server;
}

Server() {
// TODO Auto-generated constructor stub
try {
serverSocket = new ServerSocket(54444);
recCours = new LinkedBlockingQueue<CDRecCourseDisplay>(10);
clientMails = new LinkedList<ClientMail>();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.start();
}

private void start(){
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true){
try {
Socket socket = serverSocket.accept();
new Thread(new Client(socket)).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}).start();
}

class Client implements Runnable{

Socket socket;
DataInputStream in;
DataOutputStream out;

public Client(Socket socket) {
// TODO Auto-generated constructor stub
this.socket = socket;
if(socket == null) return;
try {
InputStream i = socket.getInputStream();
OutputStream o = socket.getOutputStream();
in = new DataInputStream(i);
out = new DataOutputStream(o);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public void run() {
// TODO Auto-generated method stub
new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
out.writeUTF("Test Message");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();

new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
String buf = in.readUTF();
Log.d("MESSAGE", buf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}

}

和客户

class Client implements Runnable{

Socket socket;
DataInputStream in;
DataOutputStream out;

public void run() {
// TODO Auto-generated method stub
boolean conected = false;
while(!conected){
try {
Thread.sleep(500);
socket = new Socket("213.233.216.25", 54444);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
conected = true;
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
Log.e("ERROR :", e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("ERROR :", e.getMessage());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.e("ERROR :", e.getMessage());
}
}

new Thread(new Runnable() {

public void run() {
// TODO Auto-generated method stub
while(true){
try {
String buf = in.readUTF();
log.d("MESSAGE", buf);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();


new Thread(new Runnable() {

public void run() {
// TODO Auto-generated method stub
while(true){
try {
out.writeUTF("Test message from the phone");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
}

最佳答案

大多数网络(Wifi 和 3G)使用 NAT . NAT 允许出站连接,但阻止入站(互联网到设备)连接。

当您的服务器和设备都在同一个网络上时,就像您的情况一样,这会起作用,因为您没有穿越 NAT 网关。

理由:您尝试执行的操作(从互联网连接到设备)在大多数网络中都不起作用。

关于Android 客户端,PC 服务器套接字不能超过 3g,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6748435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com