简体   繁体   English

Ubuntu中C ++中的套接字编程

[英]Socket programming in C++ in Ubuntu

I have tried to implement a program using sockets in Ubuntu 10.04. 我试图在Ubuntu 10.04中使用套接字实现程序。 Here is the code: 这是代码:

#include <iostream>
#include <sys/types.h>
#include<netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <exception>
using namespace std;
using std::exception;
int main(int argc,char *argv[]){
    int sockethandle;
    if ((sockethandle=socket(AF_INET,SOCK_STREAM,IPPROTO_IP))<0)
    {
        close(sockethandle);
        exit(EXIT_FAILURE);
    }
    return 0;
}

But here are the compile errors: 但是这是编译错误:

usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/david/NetBeansProjects/socket'
/usr/bin/make  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/socket
make[2]: Entering directory `/home/david/NetBeansProjects/socket'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
main.cpp: In function ‘int main()’:
main.cpp:18: error: ‘EXIT_FAILURE’ was not declared in this scope
main.cpp:18: error: ‘exit’ was not declared in this scope
make[2]: *** [build/Debug/GNU-Linux-x86/main.o] Error 1
make[2]: Leaving directory `/home/david/NetBeansProjects/socket'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/david/NetBeansProjects/socket'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 929ms)

How can this be fixed? 如何解决?

EXIT_FAILURE and exit() are defined in <stdlib.h> since you have not included this in your module, the compiler is pointing out that it does not know what these symbols mean. EXIT_FAILUREexit()<stdlib.h>中定义,因为您尚未在模块中包括它,编译器指出它不知道这些符号的含义。

Adding: 新增:

#include <stdlib.h> 

should sort your problem out. 应该解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM