The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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-21-2008 01:08 AM
HTTP Keep-Alive socket problem imdupeng High Level Programming 0 03-29-2008 09:11 PM
Connecting with X-win problem dragunu UNIX for Dummies Questions & Answers 2 02-27-2007 10:21 AM
Socket Problem Agent007 High Level Programming 3 04-03-2004 08:15 PM
[Problem]Reuse port in BSD socket Namely High Level Programming 1 11-28-2003 11:36 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-28-2007
Stevhp Stevhp is offline
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;

}
  #2 (permalink)  
Old 04-28-2007
porter porter is offline Forum Advisor  
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.
  #3 (permalink)  
Old 04-28-2007
Stevhp Stevhp is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 9
Reply

I'm using Solaris.
  #4 (permalink)  
Old 04-28-2007
porter porter is offline Forum Advisor  
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));
  #5 (permalink)  
Old 04-29-2007
manoj.rana manoj.rana is offline
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.
  #6 (permalink)  
Old 04-29-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,206
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.
  #7 (permalink)  
Old 04-30-2007
Stevhp Stevhp is offline
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:37 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0