Problem using sockets
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Problem using sockets




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Problem using sockets  
Vlad Dogaru


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-14-07 12:20 PM

(originally posted to comp.lang.c, but was directed here since standard
C does not include support for sockets).

Hello,

I am trying to write a simple program to teach myself sockets. The
following bit of code fails with:
"connect: Socket operation on non-socket". What am I missing?

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <strings.h>
#include <sys/select.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>

int main(int argc, char **argv)
{
int s1, s2; /* socket descriptors */
extern int errno;
struct sockaddr_in sa1, sa2;
char aux[MAX_STR_LEN];
struct hostent *hp;

if (argc != 5) {
printf("Usage: %s <host1> <port1> <host2> <port2>\n", argv[0]);
return 0;
}

if ((hp=gethostbyname(argv[1])) == 0) {
perror("gethostbyname");
return errno;
}

bzero(&sa1, sizeof sa1);
sa1.sin_family = AF_INET;
sa1.sin_port   = htons(atoi(argv[2]));
sa1.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;

if ((s1=socket(AF_INET, SOCK_STREAM, 0) == -1)) {
perror("socket");
return errno;
}

if (connect(s1, (void *) &sa1, sizeof (sa1)) == -1) {
perror("connect");
return errno;
}
printf("Socket 1 connected");

Also, having two sockets open, can I use select() to read from them?
Can someone please point me to an example of using select() with
sockets?

Thanks in advance,
Vlad






[ Post a follow-up to this message ]



    Re: Problem using sockets  
Bjørn Augestad


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-14-07 12:20 PM

Vlad Dogaru wrote:
> (originally posted to comp.lang.c, but was directed here since standard
> C does not include support for sockets).
>
> Hello,
>
> I am trying to write a simple program to teach myself sockets. The
> following bit of code fails with:
> "connect: Socket operation on non-socket". What am I missing?
>
> #include <stdio.h>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <errno.h>
> #include <strings.h>
> #include <sys/select.h>
> #include <stdlib.h>
> #include <string.h>
> #include <unistd.h>
> #include <netdb.h>
>
> int main(int argc, char **argv)
> {
>     int s1, s2; /* socket descriptors */
>     extern int errno;
>     struct sockaddr_in sa1, sa2;
>     char aux[MAX_STR_LEN];
>     struct hostent *hp;
>
>     if (argc != 5) {
>         printf("Usage: %s <host1> <port1> <host2> <port2>\n", argv[0])
;
>         return 0;
>     }
>
>     if ((hp=gethostbyname(argv[1])) == 0) {
>         perror("gethostbyname");
>         return errno;
>     }
>
>     bzero(&sa1, sizeof sa1);
>     sa1.sin_family = AF_INET;
>     sa1.sin_port   = htons(atoi(argv[2]));
>     sa1.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
>
>     if ((s1=socket(AF_INET, SOCK_STREAM, 0) == -1)) {
---------------------------------------------^
You have a bug here, don't you? ;-)

HTH
Bjørn
[snip]





[ Post a follow-up to this message ]



    Re: Problem using sockets  
loic-dev@gmx.net


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-14-07 06:17 PM

Hello,
 
> ---------------------------------------------^
> You have a bug here, don't you? ;-)

Yes, looks like... a misplaced parenthesis... ;-)

Cheers,
Loic.






[ Post a follow-up to this message ]



    Re: Problem using sockets  
Vlad Dogaru


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-15-07 12:26 PM


Bj=F8rn Augestad wrote:
> Vlad Dogaru wrote: 
> ---------------------------------------------^
> You have a bug here, don't you? ;-)

Wow, I guess that's what I get for using a small font. Thanks for the
tip -- I wasted half a day trying to figure out what went wrong.

Vlad






[ Post a follow-up to this message ]



    Re: Problem using sockets  
Aggelos Mpimpoudis


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-07 12:28 PM

> Wow, I guess that's what I get for using a small font. Thanks for the
> tip -- I wasted half a day trying to figure out what went wrong.
>
> Vlad
>


You 'll get used to it.
Classic anger management needed situation :P:P:P





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:16 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register