Problem Connecting to Socket


 
Thread Tools Search this Thread
Top Forums Programming Problem Connecting to Socket
# 1  
Old 04-28-2007
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  
Old 04-28-2007
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  
Old 04-28-2007
Reply

I'm using Solaris.
# 4  
Old 04-29-2007
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  
Old 04-29-2007
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.
Smilie
# 6  
Old 04-29-2007
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.
Smilie
Please follow the rules and use full words. Cyberchat style of writing is not allowed.
# 7  
Old 04-30-2007
Thanks for the help

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

Steve
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in connecting to db in a loop

Im trying to run a query in multiple db thro sqlplus in a loop written n shell script. whenevr there is a problem in connecting to db, the shell script abruptly exits. My requirement is , when db connection fails, the script should print the msg and contnue to connect to the next db mentioned in... (10 Replies)
Discussion started by: anijan
10 Replies

2. IP Networking

Execution Problem with socket

Hi My socket program is to communicate between the two systems connected with lan. Always i am getting an error saying bind value is -1 or not connected. ..If i run both the server and client programs in the same machine in two diff terminals they are working but they are not working between two... (3 Replies)
Discussion started by: Gurvareddy
3 Replies

3. UNIX for Dummies Questions & Answers

Problem connecting with Reflection X 14.1

Other people at work are able to connect but I am not. I am not sure if there is a setting that I am missing. Connecting from Win7 to Solaris 10. Connection attempt log: gnome-session Connecting 144.243.90.235 via TELNET Thu Mar 29 15:15:32 2012 login: *** Password: ******* Last login: Thu... (2 Replies)
Discussion started by: SIFT3R
2 Replies

4. Programming

Debugging IO::Socket Not Connecting

I'm using perl to connect to a port on a unix box from a windows machine. The unix box is located in a remote office and I'm able to connect just fine from my desktop at work across the network. But if I try to remote to another windows machine at the same location as the unix box and try to... (5 Replies)
Discussion started by: nwboy74
5 Replies

5. Programming

Socket++ library problem.

Hi, My name is Daniel and I'm spanish, so I'm sorry if you can't undertand something becouse of my low-level english. Something stranger is happening to me with socket++ library and I don't know how to work on it. I has a library called commands.so and the sslclient is and object of that... (4 Replies)
Discussion started by: lock.cda
4 Replies

6. Programming

Problem with socket connection

I have a client /server file operation program.It works properly when i run the client and server program in the same system.but when i try to run the client in one system and server in another system i am getting an error in the cleint machine as "ERROR:Connection refused". Plz help me in this ... (1 Reply)
Discussion started by: vigneshinbox
1 Replies

7. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

8. Programming

and again, socket() related problem...

Dear All, I've searched many topics and googled many web-pages, but still I didn't found solution to this problem. I want to set timeout for connect(). The thing is, that my code works only on BSD, on Linux (tested on SuSE box) it freezes at connect() call :( bool SomeFunc(std::string... (1 Reply)
Discussion started by: sggkxv
1 Replies

9. UNIX for Dummies Questions & Answers

Connecting with X-win problem

Hello all, I am trying to connect to my Solaris 8 system with X-win. However, when i try to connect, a black screen comes up, then closes up immediatley. I have checked /var/dt/Xerrors, and I am getting the folllowing: Warning: Missing charsets in String to FontSet conversion Warning:... (2 Replies)
Discussion started by: dragunu
2 Replies

10. Programming

Socket Problem

Hi all, I have developed server/client application (using C) and tested it on the same machine .. but when I deploy them on different machines I get connection timeout. Well .. server machine and client machine exists on different network segments, so there is a linux firewall box to route... (3 Replies)
Discussion started by: Agent007
3 Replies
Login or Register to Ask a Question