The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
problem with socket reading swap007 UNIX for Advanced & Expert Users 2 05-20-2008 10:08 PM
HTTP Keep-Alive socket problem imdupeng High Level Programming 0 03-29-2008 05:11 PM
Connecting with X-win problem dragunu UNIX for Dummies Questions & Answers 2 02-27-2007 06:21 AM
Socket Problem Agent007 High Level Programming 3 04-03-2004 05:15 PM
[Problem]Reuse port in BSD socket Namely High Level Programming 1 11-28-2003 07:36 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-28-2007
Registered User
 

Join Date: Mar 2007
Posts: 9
Problem Connecting to Socket

Can anyone help? I'm trying to write a program which will write to a socket. I can get the server to run, but always get an error when I try to connect.

It gives me an error at the "connect" command.

It's probably a simple error, but I can't seem to find it.



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

int main(int argc, char* argv[]) {

int outgoing, newSock, i;
struct sockaddr_in sad;
char buf;
char action[255];
FILE *command;

if(argc < 4) {
printf("Wrong command line arguments");
return 1;
}

outgoing=socket(PF_INET,SOCK_STREAM,0);
if(outgoing == -1) {
printf("Unable to create socket");
return 1;
}

sad.sin_family=AF_INET;
sad.sin_port=atoi(argv[2]);
sad.sin_addr.s_addr=inet_addr(argv[1]);

if(connect(outgoing, (struct sockaddr *) &sad, sizeof(struct sockaddr_in)) == -1) {
printf("Cannot connect to address %s port %s", argv[1], argv[2]);
return 1;
}

send(outgoing, argv[3], strlen(argv[3]), 0);
while(recv(outgoing, &buf, 1, 0))
printf("%c", buf);

return 0;

}
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-28-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by Stevhp
sad.sin_family=AF_INET;
sad.sin_port=atoi(argv[2]);
sad.sin_addr.s_addr=inet_addr(argv[1]);
Let's try a wild guess, you are using a little-endian machine such as a x86?

unsigned short my_port=atoi(argv[2]);

memset(&sad,0,sizeof(sad));
sad.sin_family=AF_INET;
sad.sin_port=htons(my_port);
sad.sin_addr.s_addr=inet_addr(argv[1]);

as long as both port and host are numeric, else use getservbyname and gethostbyname.
Reply With Quote
  #3 (permalink)  
Old 04-28-2007
Registered User
 

Join Date: Mar 2007
Posts: 9
Reply

I'm using Solaris.
Reply With Quote
  #4 (permalink)  
Old 04-28-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by Stevhp
I'm using Solaris.
1. That doesn't indicate whether it's big endian or little as Solaris runs on both SPARC and x86.

2. Put "perror("connect") in the path immediately following a failed connect.

3. Do netstat -a on the target machine and confirm that there really is a LISTEN on the port you are trying to connect to.

4. before the connect() do the following

printf("connect(%s:%d)\n",
inet_ntoa(sad.sin_addr),ntohs(sad.sin_port));
Reply With Quote
  #5 (permalink)  
Old 04-29-2007
Registered User
 

Join Date: Mar 2007
Posts: 16
socket error

Hi,
apart from the previous solutions given to u.
one thing i want to add if u are still facing some error
then please use these options at the time of compilations
-lnsl -lsocket -lresolv and i hope then u r program will work fine.
Reply With Quote
  #6 (permalink)  
Old 04-29-2007
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,496
Quote:
Originally Posted by manoj.rana
Hi,
apart from the previous solutions given to u.
one thing i want to add if u are still facing some error
then please use these options at the time of compilations
-lnsl -lsocket -lresolv and i hope then u r program will work fine.
Please follow the rules and use full words. Cyberchat style of writing is not allowed.
Reply With Quote
  #7 (permalink)  
Old 04-30-2007
Registered User
 

Join Date: Mar 2007
Posts: 9
Thanks for the help

Thanks everyone for the help. After some wrestling with it, it started working. Apparently there was no server listening.

Steve
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0