TCP Client - Server : 10.10.1.15 to 10.10.1.4
Good Morning everybody ,
I wish to return to my TCP Client/Server question.
(server on 10.10.1.4 port 9888) (and client on 10.10.1.15) .
The following is my Server & then (at the botttom) my client :-
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//Server
#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
}
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//Client
#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)
{
int socketDescriptor;
struct sockaddr_in serverAddr;
char sendline[MAXLINE],recvline[MAXLINE];
// Checking if the command line argument is given
if (argc!=2)
printf("Needs the IP Address as an argument ... \n");
//Initializing the swerver socket address structure to 0
memset(&serverAddr,0,sizeof(serverAddr));
//Creation of the socket
socketDescriptor=socket(AF_INET,SOCK_STREAM,0);
//Initialise the socket address data
serverAddr.sin_family = AF_INET;
serverAddr.sin_port=htons(SERVERPORT);
//setting up the presentation format for the socket address
inet_pton(AF_INET,argv[1],&serverAddr.sin_addr);
//Connecting to the server
connect(socketDescriptor,(SOCKADDR*)&serverAddr,sizeof(serverAddr));
//Reading from the user and sending to server
while (fgets(sendline,MAXLINE,stdin)!=NULL)
{
write(socketDescriptor,sendline,strlen(sendline));
if (read(socketDescriptor,recvline,MAXLINE) == 0)
printf("Server terminated prematurely ... \n");
fputs(recvline,stdout);
}
exit (0);
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
The Server and Client connect and listen ,etc ,
BUT no data passes . This is an echo server , ie :
the client writes to the server , and the server echoes it back .
I shall be grateful for any help . I must mention that I am
already getting help from veterans on the list.My Thanks
to them for listening , and for further help .
Warm Regards,
(shyamk@eth.net)
Shyam
Reply to: