Solved

socket programming

Posted on 2006-06-20
9
375 Views
Last Modified: 2008-03-17
hi!

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 ;
}
0
Comment
Question by:cc16
9 Comments
 
LVL 3

Expert Comment

by:Dimkov
Comment Utility
I tried the code in VS2003 and it worked fine. Which files and libraries do you include?
0
 
LVL 32

Accepted Solution

by:
jhance earned 500 total points
Comment Utility
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?
0
 
LVL 1

Expert Comment

by:rongasa
Comment Utility
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.
0
 

Author Comment

by:cc16
Comment Utility
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:cc16
Comment Utility
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.

Thanks!
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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.
0
 

Author Comment

by:cc16
Comment Utility
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?
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
errno = 24 -> TOO MANY FILES OPEN
0
 

Author Comment

by:cc16
Comment Utility
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.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maxMirror challenge 10 88
Replace a tag with sed 2 41
strDist challenge 35 84
how to send memory stream from ics Client To ics server ? 11 47
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now