client.c
reset; gcc -g -Wall -ansi -pedantic client.c -o tt
Code:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
int main(void)
{
int fd, ret, nread;
struct sockaddr_in addr;
char buffer[50]="aa\nbb\ncc\ndd\nee\n";
socklen_t addrlen;
char * pch;
strcat(buffer,"LST\n");
strcat(buffer,"555\n");
if((fd=socket(AF_INET,SOCK_DGRAM,0))==-1) exit(1);
memset((void*)&addr,(int)'\0',sizeof(addr));
addr.sin_family=AF_INET;
inet_pton(AF_INET, "127.0.0.1", &(addr.sin_addr));
/*addr.sin_addr.s_addr=htonl(INADDR_ANY); * ???*/
addr.sin_port=htons(9000);
printf("strlen(buffer) =%d",strlen(buffer));
ret=sendto(fd,buffer, strlen(buffer),0,(struct sockaddr*)&addr,addrlen);
if(ret==-1)exit(1);
pch = strtok (buffer,"\n");
while (pch != NULL)
{
printf ("strtok = %s\n",pch);
pch = strtok (NULL,"\n");
}
return 0;
while(1)
{
addrlen=sizeof(addr);
nread=recvfrom(fd,buffer,128,0,(struct sockaddr*)&addr,&addrlen);
if(nread==-1)exit(1);
ret=sendto(fd,buffer,nread,0,(struct sockaddr*)&addr,addrlen);
if(ret==-1)exit(1);
}
}
server.c
reset; gcc -g -Wall -ansi -pedantic server.c -o tt
Code:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
int main(void)
{
int fd, ret, nread;
struct sockaddr_in addr;
char buffer[128];
socklen_t addrlen;
char * pch;
if((fd=socket(AF_INET,SOCK_DGRAM,0))==-1) exit(1);
memset((void*)&addr,(int)'\0',sizeof(addr));
addr.sin_family=AF_INET;
inet_pton(AF_INET, "127.0.0.1", &(addr.sin_addr));
/*addr.sin_addr.s_addr=htonl(INADDR_ANY); * ???*/
addr.sin_port=htons(9000);
ret=bind(fd,(struct sockaddr*)&addr,sizeof(addr));
if(ret==-1)exit(1);
addrlen=sizeof(addr);
nread=recvfrom(fd,buffer,128,0,(struct sockaddr*)&addr,&addrlen);
if(nread==-1)exit(1);
pch = strtok (buffer,"\n");
while (pch != NULL)
{
printf ("strtok = %s\n",pch);
pch = strtok (NULL,"\n");
}
return 0;
while(1)
{
addrlen=sizeof(addr);
nread=recvfrom(fd,buffer,128,0,(struct sockaddr*)&addr,&addrlen);
if(nread==-1)exit(1);
ret=sendto(fd,buffer,nread,0,(struct sockaddr*)&addr,addrlen);
if(ret==-1)exit(1);
}
}
hi,
i have those pieces of code of server client code. they are not working for some reason , smb knows why?