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;
}
10 More Discussions You Might Find Interesting
1. Programming
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
2. UNIX for Dummies Questions & Answers
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
3. Programming
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
4. UNIX for Advanced & Expert Users
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
5. Programming
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
6. Programming
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
7. Programming
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
8. UNIX for Dummies Questions & Answers
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
9. IP Networking
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
10. Shell Programming and Scripting
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
LEARN ABOUT LINUX
getservent_r
GETSERVENT_R(3) Linux Programmer's Manual GETSERVENT_R(3)
NAME
getservent_r, getservbyname_r, getservbyport_r - get service entry (reentrant)
SYNOPSIS
#include <netdb.h>
int getservent_r(struct servent *result_buf, char *buf,
size_t buflen, struct servent **result);
int getservbyname_r(const char *name, const char *proto,
struct servent *result_buf, char *buf,
size_t buflen, struct servent **result);
int getservbyport_r(int port, const char *proto,
struct servent *result_buf, char *buf,
size_t buflen, struct servent **result);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
getservent_r(), getservbyname_r(), getservbyport_r():
_BSD_SOURCE || _SVID_SOURCE
DESCRIPTION
The getservent_r(), getservbyname_r(), and getservbyport_r() functions are the reentrant equivalents of, respectively, getservent(3), get-
servbyname(3), and getservbyport(3). They differ in the way that the servent structure is returned, and in the function calling signature
and return value. This manual page describes just the differences from the nonreentrant functions.
Instead of returning a pointer to a statically allocated servent structure as the function result, these functions copy the structure into
the location pointed to by result_buf.
The buf array is used to store the string fields pointed to by the returned servent structure. (The nonreentrant functions allocate these
strings in static storage.) The size of this array is specified in buflen. If buf is too small, the call fails with the error ERANGE, and
the caller must try again with a larger buffer. (A buffer of length 1024 bytes should be sufficient for most applications.)
If the function call successfully obtains a service record, then *result is set pointing to result_buf; otherwise, *result is set to NULL.
RETURN VALUE
On success, these functions return 0. On error, they return one of the positive error numbers listed in errors.
On error, record not found (getservbyname_r(), getservbyport_r()), or end of input (getservent_r()) result is set to NULL.
ERRORS
ENOENT (getservent_r()) No more records in database.
ERANGE buf is too small. Try again with a larger buffer (and increased buflen).
CONFORMING TO
These functions are GNU extensions. Functions with similar names exist on some other systems, though typically with different calling sig-
natures.
EXAMPLE
The program below uses getservbyport_r() to retrieve the service record for the port and protocol named in its first command-line argument.
If a third (integer) command-line argument is supplied, it is used as the initial value for buflen; if getservbyport_r() fails with the
error ERANGE, the program retries with larger buffer sizes. The following shell session shows a couple of sample runs:
$ ./a.out 7 tcp 1
ERANGE! Retrying with larger buffer
getservbyport_r() returned: 0 (success) (buflen=87)
s_name=echo; s_proto=tcp; s_port=7; aliases=
$ ./a.out 77777 tcp
getservbyport_r() returned: 0 (success) (buflen=1024)
Call failed/record not found
Program source
#define _GNU_SOURCE
#include <ctype.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define MAX_BUF 10000
int
main(int argc, char *argv[])
{
int buflen, erange_cnt, port, s;
struct servent result_buf;
struct servent *result;
char buf[MAX_BUF];
char *protop;
char **p;
if (argc < 3) {
printf("Usage: %s port-num proto-name [buflen]
", argv[0]);
exit(EXIT_FAILURE);
}
port = htons(atoi(argv[1]));
protop = (strcmp(argv[2], "null") == 0 ||
strcmp(argv[2], "NULL") == 0) ? NULL : argv[2];
buflen = 1024;
if (argc > 3)
buflen = atoi(argv[3]);
if (buflen > MAX_BUF) {
printf("Exceeded buffer limit (%d)
", MAX_BUF);
exit(EXIT_FAILURE);
}
erange_cnt = 0;
do {
s = getservbyport_r(port, protop, &result_buf,
buf, buflen, &result);
if (s == ERANGE) {
if (erange_cnt == 0)
printf("ERANGE! Retrying with larger buffer
");
erange_cnt++;
/* Increment a byte at a time so we can see exactly
what size buffer was required */
buflen++;
if (buflen > MAX_BUF) {
printf("Exceeded buffer limit (%d)
", MAX_BUF);
exit(EXIT_FAILURE);
}
}
} while (s == ERANGE);
printf("getservbyport_r() returned: %s (buflen=%d)
",
(s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
strerror(s), buflen);
if (s != 0 || result == NULL) {
printf("Call failed/record not found
");
exit(EXIT_FAILURE);
}
printf("s_name=%s; s_proto=%s; s_port=%d; aliases=",
result_buf.s_name, result_buf.s_proto,
ntohs(result_buf.s_port));
for (p = result_buf.s_aliases; *p != NULL; p++)
printf("%s ", *p);
printf("
");
exit(EXIT_SUCCESS);
}
SEE ALSO
getservent(3), services(5)
COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU
2010-09-10 GETSERVENT_R(3)