socket programming


i would like to create 65535 sockets so that i can make a connection to every port on a computer.

it works for an array of, say, 10 sockets but when i try with a large number the socket() function call returns -1.

below is some of my code... please tell me why it is not working and how i can achieve the goal of having a large number of sockets working at the same time. thanks.

# define MAX_PORT            65535

int                  soc [ MAX_PORT ] ;
int                     index ;

for ( index = 0 ; index < MAX_PORT ; index ++ )
      soc [ index ] = socket ( AF_INET , SOCK_STREAM , IPPROTO_TCP ) ;
      if ( soc [ index ] == ERROR ) return ;
Who is Participating?
jhanceConnect With a Mentor Commented:
When you get an error from a function call like socket() it's important to check the ERROR CODE that will often give you more information about WHY the error happened.

To quote from the MSDN docs:

Return Values
If no error occurs, socket returns a descriptor referencing the new socket. Otherwise, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.

Have you called WSAGetLastError() or GetLastError() on a non-Windows platform?
I tried the code in VS2003 and it worked fine. Which files and libraries do you include?
Ok, the expected error that i would expect to occure (since you try to use all ports) is that one of the ports is being already used from another application, probably this explains why the first expert has managed to  have the application running normally.

However, if i get correct what you wish to do, is to have a server that listens to any port. in that case check in web/books about PORT_ANY in socket bind.
 I think this is more practical than opening socket for any port number.
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

cc16Author Commented:
Hey guys thanks for the replies.

Dimkov, I'm using Unix.

# include <stdio.h>
# include <string.h>
# include <unistd.h>
# include <fcntl.h>
# include <sys/time.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <netinet/in.h>

jhance, good idea. I'll try out GetLastError and post my results some time later today or tomorrow.

rongasa, actually i'm not trying to write a server. I'm writing a client that will connect to every port. I made a bet with a friend about which of us could write the faster network scanning program.
cc16Author Commented:
Hmmm. I've searched everywhere on the net for GetLastError on unix platform, but I can't find anything.

Please tell me how I can use this function from linux.

GetLastError() is a WINDOWS API.  On Linux and unix system use the global variable errno to get the error code from the last function call.
cc16Author Commented:
ok we're getting somewhere

i included errno.h, made my scan function return errno, and made my main function return the result of my scan function. i executed the program, and then found out the return value using echo $?

the error is 24

i'm trying to find out what that means but not having a lot of success.... i got my hands on errno.h but it was really short and didn't have the info i needed. couldn't find any errno.c

how can i find the meaning of this error?
errno = 24 -> TOO MANY FILES OPEN
cc16Author Commented:
yeah some guys on another forum told me about this shortly after my previous post.

i could try to change my system's per-process file descriptor limit... but i've taken their advice and decided instead to implement syn scanning using raw sockets.

thanks for the help buddy.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.