![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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; } |
|
||||
|
Quote:
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. |
|
||||
|
Quote:
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)); |
|
||||
|
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. ![]() |
|
||||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|