Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

socket programming

Posted on 2006-06-20
9
378 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
ID: 16940752
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
ID: 16940847
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
ID: 16943527
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:cc16
ID: 16948160
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
 

Author Comment

by:cc16
ID: 16949582
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
ID: 16949911
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
ID: 16950222
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
ID: 16950431
errno = 24 -> TOO MANY FILES OPEN
0
 

Author Comment

by:cc16
ID: 16952100
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

839 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