简体   繁体   中英

Socket programming in C++ in Ubuntu

I have tried to implement a program using sockets in 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.

Adding:

#include <stdlib.h> 

should sort your problem out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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