13 个解决方案
#1
通过你的ip访问啊。
电脑能访问手机就可以啊。
电脑能访问手机就可以啊。
#2
通过你的ip访问
#3
很简单,通过wifi,手机连wifi,电脑也连上wifi,这时根据ip及端口就可访问了
#4
试过了,手机输入http://IP:8080/……访问不了~ 电脑可以 手机就是不行~
#5
你的手机可以上公网吗?
#6
上公网? 手机上网有什么不一样的么?
#7
我有源代码,发给你
public class TcpSocket {
public static boolean ISCONNECT;
static Socket socket;
static OutputStream output;
static InputStream input;
private static String ip;
private static int port;
private Thread socThread = null;
private byte[] readbuffer = null;
/**
* 创建客户端到服务器的连接Socket
*
* @param ip
* @param port
* @throws IOException
*/
public TcpSocket(String ip, int port) {
TcpSocket.ip = ip;
TcpSocket.port = port;
}
public boolean SocConnect() throws IOException {
boolean re = true;
if (socket == null) {
try {
InetAddress address = InetAddress.getByName(ip);
socket = new Socket(address, port);
TcpSocket.ISCONNECT = true;
socThread = new Thread() {
@Override
public void run() {
while (!isInterrupted()) {
if (SocReceive().length > 0) {
OnReceive.OnReceive(readbuffer);
}
}
}
};
} catch (ConnectException ce) {
socket = null;
// throw ce;
re = false;
} catch (IOException e) {
SocClose();
// throw e;
re = false;
}
}
return re;
}
/**
* 发送字节数组
*
* @param bytes
* @throws IOException
*/
public void SocSend(byte[] bytes) throws IOException {
try {
output = socket.getOutputStream();
output.write(bytes);
output.flush();
// output.close();
} catch (IOException e) {
SocClose();
throw e;
}
}
/**
* 接收字节数组并填充
*
* @param bytes
* @return
* @throws IOException
*/
public byte[] SocReceive() {
int len;
byte[] readbuf = new byte[1024];
try {
if (input.available() > 0) {
len = input.available();
readbuffer = new byte[len];
int count = 0;
while (count < readbuffer.length) {
count += input.read(readbuffer, count, readbuffer.length
- count);
}
return readbuffer;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return readbuffer;
}
public int receive(byte[] bytes) throws IOException {
int rece = 0;
try {
input = socket.getInputStream();
rece = input.read(bytes);
OnReceive.OnReceive(bytes);
// input.close();
return rece;
} catch (IOException e) {
SocClose();
throw e;
}
}
public void SocClose() throws IOException {
try {
if (output != null)
output.close();
if (input != null)
input.close();
if (socket != null) {
socket.close();
socket = null;
}
TcpSocket.ISCONNECT = false;
if (null != socThread) {
socThread.stop();
socThread = null;
}
} catch (IOException e) {
throw e;
}
}
public SocOnReceiveDataHandleEvent OnReceive = null;
public interface SocOnReceiveDataHandleEvent {
public void OnReceive(byte[] bytes);
}
public SocOnReceiveDataHandleEvent getOnReceive() {
return OnReceive;
}
public void setOnReceive(SocOnReceiveDataHandleEvent onReceive) {
OnReceive = onReceive;
}
}
public class TcpSocket {
public static boolean ISCONNECT;
static Socket socket;
static OutputStream output;
static InputStream input;
private static String ip;
private static int port;
private Thread socThread = null;
private byte[] readbuffer = null;
/**
* 创建客户端到服务器的连接Socket
*
* @param ip
* @param port
* @throws IOException
*/
public TcpSocket(String ip, int port) {
TcpSocket.ip = ip;
TcpSocket.port = port;
}
public boolean SocConnect() throws IOException {
boolean re = true;
if (socket == null) {
try {
InetAddress address = InetAddress.getByName(ip);
socket = new Socket(address, port);
TcpSocket.ISCONNECT = true;
socThread = new Thread() {
@Override
public void run() {
while (!isInterrupted()) {
if (SocReceive().length > 0) {
OnReceive.OnReceive(readbuffer);
}
}
}
};
} catch (ConnectException ce) {
socket = null;
// throw ce;
re = false;
} catch (IOException e) {
SocClose();
// throw e;
re = false;
}
}
return re;
}
/**
* 发送字节数组
*
* @param bytes
* @throws IOException
*/
public void SocSend(byte[] bytes) throws IOException {
try {
output = socket.getOutputStream();
output.write(bytes);
output.flush();
// output.close();
} catch (IOException e) {
SocClose();
throw e;
}
}
/**
* 接收字节数组并填充
*
* @param bytes
* @return
* @throws IOException
*/
public byte[] SocReceive() {
int len;
byte[] readbuf = new byte[1024];
try {
if (input.available() > 0) {
len = input.available();
readbuffer = new byte[len];
int count = 0;
while (count < readbuffer.length) {
count += input.read(readbuffer, count, readbuffer.length
- count);
}
return readbuffer;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return readbuffer;
}
public int receive(byte[] bytes) throws IOException {
int rece = 0;
try {
input = socket.getInputStream();
rece = input.read(bytes);
OnReceive.OnReceive(bytes);
// input.close();
return rece;
} catch (IOException e) {
SocClose();
throw e;
}
}
public void SocClose() throws IOException {
try {
if (output != null)
output.close();
if (input != null)
input.close();
if (socket != null) {
socket.close();
socket = null;
}
TcpSocket.ISCONNECT = false;
if (null != socThread) {
socThread.stop();
socThread = null;
}
} catch (IOException e) {
throw e;
}
}
public SocOnReceiveDataHandleEvent OnReceive = null;
public interface SocOnReceiveDataHandleEvent {
public void OnReceive(byte[] bytes);
}
public SocOnReceiveDataHandleEvent getOnReceive() {
return OnReceive;
}
public void setOnReceive(SocOnReceiveDataHandleEvent onReceive) {
OnReceive = onReceive;
}
}
#8
直接通过wifi来连接到自己的服务器端,把ip地址设置成自己的主机本地ip,端口号看自己tomcat设置多少
#9
解决了,原来是端口的问题,不知道为什么学校的IP端口设成8080和80,手机就无法访问
#10
默认端口号一般都是80,tomcat有时候默认的就是8080这个看自己的需求了,www网址的网站一般都是80端口可以自动隐藏,局域网一般自己设置自己的端口,自己访问比较方便
#11
楼主,这该怎么做啊,我现在是模拟 器上能访问服务端,到手机上就不行。
#12
哥们,,,可能只是电脑上的防火墙级别太高。
关掉防火墙就好了。我也郁闷了好久
关掉防火墙就好了。我也郁闷了好久
#13
搞了那么久才知道。是防火墙的问题。。
#1
通过你的ip访问啊。
电脑能访问手机就可以啊。
电脑能访问手机就可以啊。
#2
通过你的ip访问
#3
很简单,通过wifi,手机连wifi,电脑也连上wifi,这时根据ip及端口就可访问了
#4
试过了,手机输入http://IP:8080/……访问不了~ 电脑可以 手机就是不行~
#5
你的手机可以上公网吗?
#6
上公网? 手机上网有什么不一样的么?
#7
我有源代码,发给你
public class TcpSocket {
public static boolean ISCONNECT;
static Socket socket;
static OutputStream output;
static InputStream input;
private static String ip;
private static int port;
private Thread socThread = null;
private byte[] readbuffer = null;
/**
* 创建客户端到服务器的连接Socket
*
* @param ip
* @param port
* @throws IOException
*/
public TcpSocket(String ip, int port) {
TcpSocket.ip = ip;
TcpSocket.port = port;
}
public boolean SocConnect() throws IOException {
boolean re = true;
if (socket == null) {
try {
InetAddress address = InetAddress.getByName(ip);
socket = new Socket(address, port);
TcpSocket.ISCONNECT = true;
socThread = new Thread() {
@Override
public void run() {
while (!isInterrupted()) {
if (SocReceive().length > 0) {
OnReceive.OnReceive(readbuffer);
}
}
}
};
} catch (ConnectException ce) {
socket = null;
// throw ce;
re = false;
} catch (IOException e) {
SocClose();
// throw e;
re = false;
}
}
return re;
}
/**
* 发送字节数组
*
* @param bytes
* @throws IOException
*/
public void SocSend(byte[] bytes) throws IOException {
try {
output = socket.getOutputStream();
output.write(bytes);
output.flush();
// output.close();
} catch (IOException e) {
SocClose();
throw e;
}
}
/**
* 接收字节数组并填充
*
* @param bytes
* @return
* @throws IOException
*/
public byte[] SocReceive() {
int len;
byte[] readbuf = new byte[1024];
try {
if (input.available() > 0) {
len = input.available();
readbuffer = new byte[len];
int count = 0;
while (count < readbuffer.length) {
count += input.read(readbuffer, count, readbuffer.length
- count);
}
return readbuffer;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return readbuffer;
}
public int receive(byte[] bytes) throws IOException {
int rece = 0;
try {
input = socket.getInputStream();
rece = input.read(bytes);
OnReceive.OnReceive(bytes);
// input.close();
return rece;
} catch (IOException e) {
SocClose();
throw e;
}
}
public void SocClose() throws IOException {
try {
if (output != null)
output.close();
if (input != null)
input.close();
if (socket != null) {
socket.close();
socket = null;
}
TcpSocket.ISCONNECT = false;
if (null != socThread) {
socThread.stop();
socThread = null;
}
} catch (IOException e) {
throw e;
}
}
public SocOnReceiveDataHandleEvent OnReceive = null;
public interface SocOnReceiveDataHandleEvent {
public void OnReceive(byte[] bytes);
}
public SocOnReceiveDataHandleEvent getOnReceive() {
return OnReceive;
}
public void setOnReceive(SocOnReceiveDataHandleEvent onReceive) {
OnReceive = onReceive;
}
}
public class TcpSocket {
public static boolean ISCONNECT;
static Socket socket;
static OutputStream output;
static InputStream input;
private static String ip;
private static int port;
private Thread socThread = null;
private byte[] readbuffer = null;
/**
* 创建客户端到服务器的连接Socket
*
* @param ip
* @param port
* @throws IOException
*/
public TcpSocket(String ip, int port) {
TcpSocket.ip = ip;
TcpSocket.port = port;
}
public boolean SocConnect() throws IOException {
boolean re = true;
if (socket == null) {
try {
InetAddress address = InetAddress.getByName(ip);
socket = new Socket(address, port);
TcpSocket.ISCONNECT = true;
socThread = new Thread() {
@Override
public void run() {
while (!isInterrupted()) {
if (SocReceive().length > 0) {
OnReceive.OnReceive(readbuffer);
}
}
}
};
} catch (ConnectException ce) {
socket = null;
// throw ce;
re = false;
} catch (IOException e) {
SocClose();
// throw e;
re = false;
}
}
return re;
}
/**
* 发送字节数组
*
* @param bytes
* @throws IOException
*/
public void SocSend(byte[] bytes) throws IOException {
try {
output = socket.getOutputStream();
output.write(bytes);
output.flush();
// output.close();
} catch (IOException e) {
SocClose();
throw e;
}
}
/**
* 接收字节数组并填充
*
* @param bytes
* @return
* @throws IOException
*/
public byte[] SocReceive() {
int len;
byte[] readbuf = new byte[1024];
try {
if (input.available() > 0) {
len = input.available();
readbuffer = new byte[len];
int count = 0;
while (count < readbuffer.length) {
count += input.read(readbuffer, count, readbuffer.length
- count);
}
return readbuffer;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return readbuffer;
}
public int receive(byte[] bytes) throws IOException {
int rece = 0;
try {
input = socket.getInputStream();
rece = input.read(bytes);
OnReceive.OnReceive(bytes);
// input.close();
return rece;
} catch (IOException e) {
SocClose();
throw e;
}
}
public void SocClose() throws IOException {
try {
if (output != null)
output.close();
if (input != null)
input.close();
if (socket != null) {
socket.close();
socket = null;
}
TcpSocket.ISCONNECT = false;
if (null != socThread) {
socThread.stop();
socThread = null;
}
} catch (IOException e) {
throw e;
}
}
public SocOnReceiveDataHandleEvent OnReceive = null;
public interface SocOnReceiveDataHandleEvent {
public void OnReceive(byte[] bytes);
}
public SocOnReceiveDataHandleEvent getOnReceive() {
return OnReceive;
}
public void setOnReceive(SocOnReceiveDataHandleEvent onReceive) {
OnReceive = onReceive;
}
}
#8
直接通过wifi来连接到自己的服务器端,把ip地址设置成自己的主机本地ip,端口号看自己tomcat设置多少
#9
解决了,原来是端口的问题,不知道为什么学校的IP端口设成8080和80,手机就无法访问
#10
默认端口号一般都是80,tomcat有时候默认的就是8080这个看自己的需求了,www网址的网站一般都是80端口可以自动隐藏,局域网一般自己设置自己的端口,自己访问比较方便
#11
楼主,这该怎么做啊,我现在是模拟 器上能访问服务端,到手机上就不行。
#12
哥们,,,可能只是电脑上的防火墙级别太高。
关掉防火墙就好了。我也郁闷了好久
关掉防火墙就好了。我也郁闷了好久
#13
搞了那么久才知道。是防火墙的问题。。