Network Programming in C on Linux ... Pls Help
Dear members ,
I am having some serious doubts (generated by a vague book
and my ignorance/stupidity combined together).
1) First , I wish to talk about the memset function
used in the TCPEchoServer.c program .
This function takes 3 arguments as far as the documentation is concerned , but the
program (and all programs using this function ,) just pass 2 arguments. This gives us
the following error :
tcpechoserv.c : In function 'main' :
tcpechoserv :22 : too few arguments to function 'memset'
When I supplied a 0 for the (hitherto) unspecified parameter , I get complaints
about the readline function.
I have attached the text file containing the tcpechoserv.c as it is typed in the Text
.
2) The next question is regarding the UdpEchoServer.c
I am getting the error :
udpechoserver.c : In function 'main' :
udpechoserv.c : 33 : incompatible type for argument 5 of 'sendto'
This is what the sendto function looks like :
sendto(socketDescriptor,line,n,0,clientAddr,clientAddrLen);
In the same book from which I took all these , there is a
topic on RPC contains little instruction on what to do .
a)It gives one msg.x ,file (which anyway rpcgen shows an error :
msg.x : line 5: expected 'version' or '}' )
b)What do we do with the server C program , that follows the msg.x file (a msg-proc.c
file)?
c) And they have not mentioned any .x files for the client
d) What What do we do with the server C program shown for the client ?
e)They show in the next 2 pages the C programs that rpcgen generates ,
but it remains vague , because ,the text never manages to take our programs there.
Could somebody give me pointers to good sites where I can get good code
examples in RPC server/clients ? Or could somebody give me some code examples (I
guess
it takes 6 files: 1 .x file and .c file for the server , and 1 .x and .c file
for the client
+ the 2 .c files that rpcgen is supposed to generate .)
Sincerely,
(shyamk@eth.net)
Shyam
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define SOCKADDR struct sockaddr
#define SERVERPORT 9888
#define BACKLOG 1024
#define MAXLINE 4096
void main(int argc,char **argv)
{
//Declarations for required variables
int socketDescriptor,connDescriptor;
pid_t childpid;
socklen_t clientAddrLen;
struct sockaddr_in clientAddr,serverAddr;
ssize_t n;
char line[MAXLINE];
//Initialize the socket address structure to 0
memset(&serverAddr,sizeof(serverAddr));
//Create the socket
socketDescriptor=socket(AF_INET,SOCK_STREAM,0);
//Initialize the server socket address structure
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr=INADDR_ANY;
serverAddr.sin_port=htons(SERVERPORT);
//Bind the socket to a unique name
bind(socketDescriptor,(SOCKADDR*)&serverAddr,sizeof(serverAddr));
//Ready to accept connections
listen(socketDescriptor,BACKLOG);
//Do the task
for(;;)
{
clientAddrLen=sizeof(clientAddr);
connDescriptor=accept(socketDescriptor,(SOCKADDR*)&clientAddr,&clientAddrLen);
if((childpid= fork()) == 0) /*child process */
{
close(socketDescriptor);
for(;;)
{
if ((n = readline(connDescriptor,line,MAXLINE))==0)
return; //connection closed
write(connDescriptor,line,n);
}
exit(0);
}
close(connDescriptor); //connected socket closed
}
}
Reply to: