
/ We have to set the callback here even though
the new socket is unknown conn->socket is marked as -1 /
newconn = netconn_alloc(conn->type, conn->callback);
if (newconn == NULL) {
return ERR_MEM;
}
这里返回的 ERR_MEM 导致后面的问题,也就是内存分配出问题了。
到 netconn_alloc() 函数中发现 MEMP_NETCONN 好像没有定义。。。
1、服务器端
import javaioDataOutputStream;import javaioIOException;
import javanetServerSocket;
import javanetSocket;
public class SocketServer {
private static final int PORT = 8088;
public static void main(String[] args) {
ServerSocket server = null;
try {
server = new ServerSocket(PORT);
while (true) {
Socket client = serveraccept();
new Thread(new Server(client))start();
}
} catch (IOException e) {
eprintStackTrace();
}
}
}
class Server implements Runnable {
private Socket client;
public Server(Socket client) {
thisclient = client;
}
public void run() {
DataOutputStream output = null;
try {
output = new DataOutputStream(clientgetOutputStream());
outputwriteUTF("你好我是服务器");
} catch (IOException e) {
eprintStackTrace();
} finally {
try {
if (output != null) outputclose();
output = null;
} catch (IOException e) {}
}
}
}
2、客户端
import javaioDataInputStream;import javaioIOException;
import javanetSocket;
import javanetUnknownHostException;
public class Client extends Socket {
private static final int PORT = 8088;
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket("127001", PORT);
DataInputStream in = new DataInputStream(socketgetInputStream());
String res = inreadUTF();
Systemoutprintln(res);
if (in != null) inclose();
} catch (UnknownHostException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
} finally {
if (socket != null) {
try {
socketclose();
} catch (IOException e) {}
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)