0
进入套接字编程我正在开发一个程序,用于在手机上运行我的android应用程序,以便基于我的机器与我的serlet进行通信。我得到一个异常说,套接字已关闭。 的servletside使用servlets进行android和servlet沟通
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);
你应该告诉更多正在发生的事情和异常到来之前被执行的语句。请发布logcat。你也应该知道哪个陈述确实会引发这个错误。你没有告诉服务器是否正确接收到IP和MAC。 – greenapps 2014-11-04 10:13:36