我刚接触socket编程,我正在开发一个程序,用于在手机上运行的Android应用程序,以便根据我的机器与我的serlet进行通信。 我得到一个例外说套接字已关闭。 servletside
private ServerSocket serverSocket;
public static final int SERVERPORT = 8080;
Socket socket=null;
serverSocket = new ServerSocket(SERVERPORT);
socket = serverSocket.accept();
InputStream is =socket.getInputStream();
DataInputStream dis =new DataInputStream(is);
String input = dis.readUTF();
data1 = dis.readUTF();
System.out.println(input+" "+data1);
OutputStream dout=socket.getOutputStream();
DataOutputStream dat = new DataOutputStream(dout);
dat.writeUTF("Hello to you too");
dat.close();
应用方
private Socket socket;
private static final int SERVERPORT = 8080;
private static final String SERVER_IP = "192.168.1.8";
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
DataOutputStream out1 = new DataOutputStream(socket.getOutputStream());
out1.writeUTF(ip);
out1.writeUTF(mac);
out1.close();
Log.d("Message","WTF");
InputStream is =socket.getInputStream();
DataInputStream dis =new DataInputStream(is);
while(dis.available()==0);
Log.d("Message","came here 2");
String WTF = dis.readUTF();
dis.close();
Log.d("Message",WTF);
答案 0 :(得分:0)
尝试将下一行放在Manifest上;
<uses-permission android:name="android.permission.INTERNET"/>