Home > Archive > Unix Programming > September 2007 > Sending Data Via a Socket
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Sending Data Via a Socket
|
|
| kvnsmnsn@hotmail.com 2007-09-28, 4:22 am |
| I'm trying to establish a conversation between two different machines
via a Unix socket, using the code below. It looks pretty clear that a
socket is being formed, since the server waits for the client to start
running, and they both announce the arrival of data just the right
number of times. But the only things each machine reads from its buf-
fer after doing a "recv()" is precisely what that machine previously
wrote to that buffer. Isn't that buffer supposed to fill up with the
data being sent across the socket? Am I somehow failing to send any
data across the socket? Any information anybody can give me on this
would be greatly appreciated.
---Kevin Simonson
"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
########################################
############################
Script started on Thu 27 Sep 2007 09:24:27 PM MDT
guitar:kvnsmnsn/Cs360_bash-3.2$ hostname
guitar
guitar:kvnsmnsn/Cs360_bash-3.2$ cat ServerSocket.c
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( int argCount
, char** arguments)
{
if (argCount == 2)
{ struct sockaddr_in addr;
memset( &addr, 0, sizeof( struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons( (u_short) atoi( arguments[ 1]));
addr.sin_addr.s_addr = INADDR_ANY;
int lstnSckt = socket( AF_INET, SOCK_STREAM, 0);
bind( lstnSckt, (struct sockaddr *) &addr, sizeof( struct
sockaddr_in));
listen( lstnSckt, 5);
struct sockaddr clientName;
int cnLength;
int sckt = accept( lstnSckt, &clientName, &cnLength);
char buffer[ 101];
recv( sckt, buffer, 100, 14);
printf( "Server received message \"%s\".\n", buffer);
strcpy( buffer, "testing 4-5-6");
printf( "Server sending message \"testing 4-5-6\".\n");
send( sckt, buffer, 100, 14);
recv( sckt, buffer, 100, 14);
printf( "Server received message \"%s\".\n", buffer);
printf( "Server closing.\n");
close( sckt);
}
else
{ printf( "Usage is\n ./ServerSocket <port>\n");
}
}
guitar:kvnsmnsn/Cs360_bash-3.2$ ./ServerSocket 3000
Server received message "".
Server sending message "testing 4-5-6".
Server received message "testing 4-5-6".
Server closing.
guitar:kvnsmnsn/Cs360_bash-3.2$ exit
exit
Script done on Thu 27 Sep 2007 09:25:58 PM MDT
########################################
############################
Script started on Thu 27 Sep 2007 09:25:22 PM MDT
drums:kvnsmnsn/Cs360_bash-3.2$ hostname
drums
drums:kvnsmnsn/Cs360_bash-3.2$ cat ClientSocket.c
#include <stdio.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( int argCount
, char** arguments)
{
if (argCount == 3)
{ struct sockaddr_in addr;
memset( &addr, 0, sizeof( struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons( (u_short) atoi( arguments[ 2]));
struct hostent* host = gethostbyname( arguments[ 1]);
memcpy( &addr.sin_addr, host->h_addr_list[ 0], host->h_length);
int sckt = socket( AF_INET, SOCK_STREAM, 0);
char buffer[ 101];
connect( sckt, (struct sockaddr *) &addr, (socklen_t)
sizeof( addr));
strcpy( buffer, "testing 1-2-3");
printf( "Client sending message \"testing 1-2-3\".\n");
send( sckt, buffer, 100, 14);
recv( sckt, buffer, 100, 14);
printf( "Client received message \"%s\".\n", buffer);
strcpy( buffer, "testing 7-8-9");
printf( "Client sending message \"testing 7-8-9\".\n");
send( sckt, buffer, 100, 14);
printf( "Client closing.\n");
close( sckt);
}
else
{ printf( "Usage is\n ./ClientSocket <dsntn-hst> <port>\n");
}
}
drums:kvnsmnsn/Cs360_bash-3.2$ ./ClientSocket guitar.cs.byu.edu 3000
Client sending message "testing 1-2-3".
Client received message "testing 1-2-3".
Client sending message "testing 7-8-9".
drums:kvnsmnsn/Cs360_bash-3.2$ exit
exit
Script done on Thu 27 Sep 2007 09:26:02 PM MDT
| |
| Scott Lurndal 2007-09-28, 7:11 pm |
| kvnsmnsn@hotmail.com writes:
>I'm trying to establish a conversation between two different machines
>via a Unix socket, using the code below. It looks pretty clear that a
>socket is being formed, since the server waits for the client to start
Nothing's clear if you don't check for errors. You also forgot to
bind on the client side.
scott
| |
|
| On Thu, 27 Sep 2007 20:32:12 -0700, kvnsmnsn wrote:
> I'm trying to establish a conversation between two different machines
> via a Unix socket, using the code below. It looks pretty clear that a
> socket is being formed, since the server waits for the client to start
> running, and they both announce the arrival of data just the right
> number of times. But the only things each machine reads from its buf-
> fer after doing a "recv()" is precisely what that machine previously
> wrote to that buffer. Isn't that buffer supposed to fill up with the
> data being sent across the socket? Am I somehow failing to send any
> data across the socket? Any information anybody can give me on this
> would be greatly appreciated.
>
> ---Kevin Simonson
>
> "You'll never get to heaven, or even to LA,
> if you don't believe there's a way."
> from _Why Not_
>
> ########################################
############################
>
> Script started on Thu 27 Sep 2007 09:24:27 PM MDT
> guitar:kvnsmnsn/Cs360_bash-3.2$ hostname
> guitar
> guitar:kvnsmnsn/Cs360_bash-3.2$ cat ServerSocket.c
> #include <arpa/inet.h>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> int main ( int argCount
> , char** arguments)
> {
> if (argCount == 2)
> { struct sockaddr_in addr;
> memset( &addr, 0, sizeof( struct sockaddr_in));
> addr.sin_family = AF_INET;
> addr.sin_port = htons( (u_short) atoi( arguments[ 1]));
> addr.sin_addr.s_addr = INADDR_ANY;
> int lstnSckt = socket( AF_INET, SOCK_STREAM, 0);
> bind( lstnSckt, (struct sockaddr *) &addr, sizeof( struct
> sockaddr_in));
> listen( lstnSckt, 5);
> struct sockaddr clientName;
> int cnLength;
> int sckt = accept( lstnSckt, &clientName, &cnLength);
> char buffer[ 101];
while (1) {
int rc;
[snip]
rc = recv( sckt, buffer, 100, 0);
what is the 14 for flags ?
if (rc <= 0) break;
> printf( "Server received message \"%s\".\n", buffer);
> strcpy( buffer, "testing 4-5-6");
> printf( "Server sending message \"testing 4-5-6\".\n");
> send( sckt, buffer, 100, 0);
What is the 14 for flags ?
>
[snip]
}
> printf( "Server closing.\n");
> close( sckt);
> }
> else
> { printf( "Usage is\n ./ServerSocket <port>\n");
> }
> }
> guitar:kvnsmnsn/Cs360_bash-3.2$ ./ServerSocket 3000
> Server received message "".
> Server sending message "testing 4-5-6".
> Server received message "testing 4-5-6".
> Server closing.
> guitar:kvnsmnsn/Cs360_bash-3.2$ exit
> exit
>
> Script done on Thu 27 Sep 2007 09:25:58 PM MDT
>
> ########################################
############################
>
> Script started on Thu 27 Sep 2007 09:25:22 PM MDT
> drums:kvnsmnsn/Cs360_bash-3.2$ hostname
> drums
> drums:kvnsmnsn/Cs360_bash-3.2$ cat ClientSocket.c
> #include <stdio.h>
> #include <arpa/inet.h>
> #include <netdb.h>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> int main ( int argCount
> , char** arguments)
> {
> if (argCount == 3)
> { struct sockaddr_in addr;
> memset( &addr, 0, sizeof( struct sockaddr_in));
> addr.sin_family = AF_INET;
> addr.sin_port = htons( (u_short) atoi( arguments[ 2]));
> struct hostent* host = gethostbyname( arguments[ 1]);
> memcpy( &addr.sin_addr, host->h_addr_list[ 0], host->h_length);
> int sckt = socket( AF_INET, SOCK_STREAM, 0);
> char buffer[ 101];
> connect( sckt, (struct sockaddr *) &addr, (socklen_t)
> sizeof( addr));
> strcpy( buffer, "testing 1-2-3");
> printf( "Client sending message \"testing 1-2-3\".\n");
> send( sckt, buffer, 100, 14);
> recv( sckt, buffer, 100, 14);
what is that 14 for flags ?
> printf( "Client received message \"%s\".\n", buffer);
> strcpy( buffer, "testing 7-8-9");
> printf( "Client sending message \"testing 7-8-9\".\n");
> send( sckt, buffer, 100, 14);
Again ...
> printf( "Client closing.\n");
> close( sckt);
> }
> else
> { printf( "Usage is\n ./ClientSocket <dsntn-hst> <port>\n");
> }
> }
> drums:kvnsmnsn/Cs360_bash-3.2$ ./ClientSocket guitar.cs.byu.edu 3000
> Client sending message "testing 1-2-3".
> Client received message "testing 1-2-3".
> Client sending message "testing 7-8-9".
> drums:kvnsmnsn/Cs360_bash-3.2$ exit
> exit
>
> Script done on Thu 27 Sep 2007 09:26:02 PM MDT
HTH,
AvK
|
|
|
|
|