本人做的是android 客户端,最近的项目中用联通的2g、3g卡都不能访问服务器、移动就正常,我写了一个测试的小例子、还是失败,代码贴上来请大家帮我查找问题
服务器:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.List;
public class TCPPCServer {
static ServerSocket ss;
static InputStream in = null;
static OutputStream out = null;
static String sendMsgA = "";
static String sendMsgB = "";
static int portA = 0;
static int portB = 0;
static SocketAddress addressA = null;
static SocketAddress addressB = null;
static List<Socket> list = null;
public static void main(String[] args) {
try {
ss = new ServerSocket(3234);
System.out.println("成功建立客户端!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (!ss.isClosed()) {
try {
Socket socket = ss.accept();
// list.add(socket);
System.out.println("有一个客户端连接来了!====本地IP:"+socket.getLocalAddress()+"\nNAT端口:"+socket.getPort()+"\nNATIP:"+socket.getInetAddress());
// in = socket.getInputStream();
// out = socket.getOutputStream();
// String message = receive();
//
// if (message.contains("A")) {
// portA = socket.getPort();
// addressA = socket.getLocalSocketAddress();
// sendMsgA = "host:" + addressA + "|" + portA;
// System.out.println(sendMsgA);
//
// }
// if (message.contains("B")) {
// portB = socket.getPort();
// addressB = socket.getLocalSocketAddress();
// sendMsgB = "host:" + addressB + "|" + portB;
// System.out.println(sendMsgB);
//
// }
// // 两个都接收到后分别A、B地址交换互发
// if (!sendMsgA.equals("") && !sendMsgB.equals("")) {
// sendMsg(addressA, portA, sendMsgA, list.get(1));
// sendMsg(addressB, portB, sendMsgB, list.get(0));
// sendMsgA = "";
// sendMsgB = "";
// }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static String receive() {
String remsg = "";
try {
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
len = in.read();
while (true) {
if ((len = in.read(buffer, 0, len)) > 0) {
remsg = new String(buffer);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return remsg;
}
private static void sendMsg(SocketAddress ip, int port, String cont,
Socket socket) {
String sendCont = ip + "," + port + "," + cont;
byte[] buffer = sendCont.getBytes();
try {
out.write(buffer);
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
里面有一部分代码是我做别的功能时候用到的注释了,希望不会影响大家的阅读
客户端:android 的
package com.example.tttt1;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button lianjie;
EditText etIP, etPort;
String IP, sPort;
int Port;
TextView status;
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:// 成功
status.setText("成功连接!");
break;
case 2:// 失败
status.setText("连接失败!");