客户端用移动能访问服务器、联通2g、3g 都不能访问
本人做的是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();
}
}
}
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("连接失败!");
break;
case 3:
Toast.makeText(MainActivity.this, "尼玛,端口必须是数字,并且不能为null",
Toast.LENGTH_LONG).show();
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lianjie = (Button) findViewById(R.id.lianjie);
etIP = (EditText) findViewById(R.id.etIP);
etPort = (EditText) findViewById(R.id.etPort);
status = (TextView) findViewById(R.id.status);
String ip = GetHostIp();
//boolean flag=pingIpAddr();
//System.out.println(flag);
initListener();
}
private void initListener() {
lianjie.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
try {
IP = etIP.getText().toString();
sPort = etPort.getText().toString();
if (sPort == null) {
Message msg = new Message();
msg.what = 3;
mHandler.sendMessage(msg);
} else {
Port = Integer.parseInt(sPort);
}
Socket socket = new Socket();
SocketAddress sd = new InetSocketAddress(IP, Port);
socket.connect(sd, 30000);
Message msg = new Message();
Log.e("1", "成功");
msg.what = 1;
mHandler.sendMessage(msg);
} catch (UnknownHostException e) {
Log.e("1", "失败1");
Message msg = new Message();
msg.what = 2;
mHandler.sendMessage(msg);
e.printStackTrace();
} catch (IOException e) {
Log.e("1", "失败2" + e);
Message msg = new Message();
msg.what = 2;
mHandler.sendMessage(msg);
e.printStackTrace();
}
}
}).start();
}
});
}
private static final boolean pingIpAddr() {
String mPingIpAddrResult;
try {
String ipAddress = "122.70.93.159";
Process p = Runtime.getRuntime().exec("ping -c 1 -w 100 " + ipAddress);
int status = p.waitFor();
if (status == 0) {
return true;
} else {
mPingIpAddrResult = "Fail: IP addr not reachable";
}
} catch (IOException e) {
mPingIpAddrResult = "Fail: IOException";
} catch (InterruptedException e) {
mPingIpAddrResult = "Fail: InterruptedException";
}
return false;
}
public String GetHostIp() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr
.hasMoreElements();) {
InetAddress inetAddress = ipAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
} catch (Exception e) {
}
return null;
}
}
[解决办法]
猜测;应该是服务端的问题吧 比如服务端没搞双线?
[解决办法]
楼主可以从如gprs的工作原理入手看看
[解决办法]
首先服务器是外网IP吧。
其次楼主还是先用联通的手机访问一下看看能不能访问网络,有可能是欠费或者联通信号差的原因。
我这边一般用wifi,刚才特意用了一下联通3G测试与服务器通信,很正常