spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Getting error code when creating a socket in Linux


errno is a thread-local global variable, defined in <errno.h>. The man page for many library functions will indicate that they return -1 on error, and set errno.

You can convert an errno value to a useful string with the strerror function.

In general, you should code like this:

#include <stdio.h>
#include <errno.h>

int main(void) {
    int s;

    s = socket(...);
    if (s < 0) {
        fprintf(stderr, "socket() failed: %s
", strerror(errno));
        exit(1);
    }
}

Alternatively, glibc's printf and friends support a %m format specifier, which is replaced with strerror(errno) (no argument is needed). So the above example could be replaced with:

    if (s < 0) {
        fprintf(stderr, "socket() failed: %m
");
        exit(1);
    }

And to make it all simpler, there is the perror function, which prints out a message similar to above.

    if (s < 0) {
        perror("socket");
        exit(1);
    }

Wrapping it all up - error handling need not be complex and verbose. Putting the socket call and the test for < 0 in one statement, the above code could look like this, and you'll be a real UNIX pro:

#include <stdio.h>
#include <errno.h>

int main(void) {
    int s;

    if ((s = socket(...)) < 0) {
        perror("socket");
        exit(1);
    }
}

Categories : C

Related to : Getting error code when creating a socket in Linux
How to bind a raw socket to a specific interface using python in linux centOS?
You can do it by using the IP address that corresponds to the desired interface. import socket s = socket.socket() s.bind(('192.168.1.100', 12345)) s = socket.socket() s.bind(('localhost', 12345)) s = socket.socket() s.bind(('0.0.0.0', 12345)) The first two above would bind to the interface with that IP address. The last one will bind to any interface. You can obtain the IP address for an in

Categories : Python
Winsock ~ Creating an UDP Listener (Multiple vs 1 socket)
Unlike TCP, UDP is connection-less, and as such you don't need to create separate sockets for each party. One UDP socket can handle everything. Bind it to a local IP/Port and call WSARecvFrom() once, and when it reports data to your IOCP you can process the data as needed (if another thread if needed) and then call WSARecvFrom() again. Each time new data arrives, you have to look at the reporte

Categories : C++
.Net Socket class error codes
If you receive a SocketException in .NET the exception has a property called ErrorCode which maps to all of the values you posted in your first link. So both API's are just as granular in their error reporting. The enum you linked to is just for convince so you don't need to make your own constants for the more popular error codes. const int WSA_E_NO_MORE= 10110; const int WSAEACCES = 10013; vo

Categories : C++
How to get socket's buffered data when socket closed due to network loss?
The socket doesn't get closed due to network loss. Only you can close your socket. The connection can be reset due to network problems. In this case all pending data is discarded. Nothing you can do about it.

Categories : Java
Gtk+ Version Error on Linux
Can you tell us wich program you try to use ? Can you Modify the source code to fit with your Gtk version ? Does it work ? The upgrade from Gtk 3.4 to 3.10 is not possible (with aptitude or apt-get); Gtk 3.10 can't be found in the official repository of debian (correct me if i'm wrong), see this link However you can install a later unstable version, see this link (It's not recommand too).

Categories : Python
Recently Add
Rewrite of IDA decompiled function
Computing the average of grades in C
passing structures to functions
Use semaphores for handling sockets in C
How to search for a string pattern inside html, coding in C?
sprintf invalid format '%d'
Why can't this c program run correctly?
Low Pass filter in C
Child process does not print anything
C: datatypes. sqrt function working with int why?
How to implement Serial Port Profile Link Command used in ConnectBlue Bluetooth Module?
Rerunning cancelled pthread
How to print out a string that is pointed to by a char pointer?
Can unverified scanf call cause an undefined behavior?
nested structures in C
Portable Makevars for R package using C, GSL and OpenMP with help of Rcpp
program doesn't run(getting RUN FINISHED Segmentation fault core dumped)
Finding the sign of a Lua number, from C
Width of symbols created by gcc's objectcopy
Strange behavior when using free in c program
Can't open image C
Strange C code - dynamic arrays?
C program printing weird characters
Fault in decimal to binary in c
How to detect broken pipe on input?
How can I ivestigate use of the register keyword in C?
File scaning with fscanf
why gcc generates "leal -8240(%ebp), %eax; cmpl $1, %eax; ja XXX" instead of jmp instruction?
Wierd Problems With My Own Custom Written Word Search Algorithm
Trouble with making a shell in C
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.