android socket连接,真机连接不上,虚拟机可以连接。用的是校内网。代码在下面。各位大神,来看看!?
packagecom.example.system.socket;importjava.io.BufferedInputStream;importjava.io.IOEx...
package com.example.system.socket;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//连接管理,单例实现,使连接与数据交互分开
public class Socketset {
private Socket con=null;
private final int PORT =30000;
private InetAddress addr;
private ObjectOutputStream oos=null;
private ObjectInputStream ois=null;
private boolean isnew1=false;
private boolean isnew2=false;
public Socketset(){
try {
addr= InetAddress.getByName("172.21.107.33");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Socket getCon(){
try{
if(con==null||con.isClosed()){
isnew1=true;
isnew2=true;
con=new Socket();
con.connect(new InetSocketAddress(addr,PORT),3000);
System.out.println("__________________________________");
}
}catch(Exception ex){
ex.printStackTrace();
con=null;
}
return con;
}
public ObjectOutputStream getOOS(){
if(isnew1){
isnew1=false;
try {
if(con!=null){
oos= new ObjectOutputStream(
con.getOutputStream());
}else{
oos=null;
}
} catch (IOException e) {
e.printStackTrace();
oos=null;
}
}
return oos;
}
public ObjectInputStream getOIS(){
if(isnew2){
isnew2=false;
try {
if(con!=null){
ois=new ObjectInputStream(
new BufferedInputStream(
con.getInputStream()));
}else{
ois=null;
}
} catch (StreamCorruptedException e) {
e.printStackTrace();
ois=null;
} catch (IOException e) {
e.printStackTrace();
ois=null;
}
}
return ois;
}
} 展开
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//连接管理,单例实现,使连接与数据交互分开
public class Socketset {
private Socket con=null;
private final int PORT =30000;
private InetAddress addr;
private ObjectOutputStream oos=null;
private ObjectInputStream ois=null;
private boolean isnew1=false;
private boolean isnew2=false;
public Socketset(){
try {
addr= InetAddress.getByName("172.21.107.33");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Socket getCon(){
try{
if(con==null||con.isClosed()){
isnew1=true;
isnew2=true;
con=new Socket();
con.connect(new InetSocketAddress(addr,PORT),3000);
System.out.println("__________________________________");
}
}catch(Exception ex){
ex.printStackTrace();
con=null;
}
return con;
}
public ObjectOutputStream getOOS(){
if(isnew1){
isnew1=false;
try {
if(con!=null){
oos= new ObjectOutputStream(
con.getOutputStream());
}else{
oos=null;
}
} catch (IOException e) {
e.printStackTrace();
oos=null;
}
}
return oos;
}
public ObjectInputStream getOIS(){
if(isnew2){
isnew2=false;
try {
if(con!=null){
ois=new ObjectInputStream(
new BufferedInputStream(
con.getInputStream()));
}else{
ois=null;
}
} catch (StreamCorruptedException e) {
e.printStackTrace();
ois=null;
} catch (IOException e) {
e.printStackTrace();
ois=null;
}
}
return ois;
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询