lwIP求助,关于socket编写TCP服务器的问题

lwIP求助,关于socket编写TCP服务器的问题,第1张

查了下,错误发生在accept_function()函数中:
/ 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) {}
            }
        }
    }
}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10686908.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-10
下一篇2023-05-10

发表评论

登录后才能评论

评论列表(0条)

    保存