Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

socket programming

Posted on 2006-06-20
9
Medium Priority
?
383 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
[X]
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
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 1500 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Six Sigma Control Plans
Starting up a Project

670 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