/*
* @filename : server.c
* @brief: Server Program
* */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define MAX_LINE 256
int conn_count=0;
void *HandlingClient(void *);
int main(int argc,char *argv[])
{
struct sockaddr_in serv_addr, client_addr;
int sockid, newsockid, len, ret;
char buf[MAX_LINE];
if(argc < 2 )
{
printf("usage: %s <port>\n", argv[0]);
return -1;
}
sockid = socket(AF_INET,SOCK_DGRAM, 0);
if(sockid < 0 )
{
printf("Failed to get socket\n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if((bind(sockid,(struct sockaddr*)&serv_addr,sizeof(serv_addr))) != 0)
{
printf("Failed to bind\n");
return -2;
}
len = sizeof(client_addr);
while(1)
{
ret = recvfrom(sockid,&buf,MAX_LINE,0, (struct sockaddr*)&client_addr, &len);
if ( ret == 0 )
{
break;
}
else if ( ret < 0 )
{
printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
break;
}
if(!strncasecmp(buf,"bye",3))
{
break;
}
/* send back to client*/
if( (ret = sendto(sockid, &buf, ret, 0, (struct sockaddr*)&client_addr, sizeof(client_addr))) <= 0 )
{
printf("Client coultn't send message to server\n");
break;
}
}
close(sockid);
return 0;
}
/*
* @filename : client.c
* @brief: Client Program
* */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAX_LINE 256
void *SendReceive(void* sockid);
int main(int argc,char *argv[])
{
struct sockaddr_in serv_addr, cli_addr ;
int sockid;
char buf[MAX_LINE],sendbuf[MAX_LINE];
int length,ret ;
socklen_t len;
if(argc < 3 )
{
printf("usage: %s <server-address> <port> \n",argv[0]);
return -1;
}
sockid = socket(AF_INET,SOCK_DGRAM,0) ;
if(sockid == -1 )
{
printf("Failed to get socket\n");
return -2;
}
serv_addr.sin_family = AF_INET ;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]) ;
serv_addr.sin_port = htons(atoi(argv[2])) ;
len = 1;
while(1)
{
memset(buf,0,sizeof(buf));
if(fgets(buf,MAX_LINE,stdin) == NULL )
{
printf ("received EOF from stdin\n");
break;
}
length = strlen(buf);
if( (ret = sendto(sockid, &buf, length, 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) <= 0 )
{
printf("Client coultn't send message to server\n");
break;
}
ret = recvfrom(sockid,&buf,MAX_LINE,0, (struct sockaddr*)&cli_addr, &len);
if ( ret == 0 )
{
printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
break;
}
else if ( ret < 0 )
{
printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
break;
}
printf("%s",buf);
}
close(sockid);
return 0;
}
MESSAGE="Echo Program Built Successfully"
MESSAGE1 = " ------------------------- "
CC = gcc
CFLAGS = -g -I./include
all: server client
@echo $(MESSAGE1)
@echo $(MESSAGE)
@echo $(MESSAGE1)
server: server.o
${CC} -o $@ server.o
client: client.o
${CC} -o $@ client.o
clean:
-@rm *.o
distclean: clean
-@rm server client
* @filename : server.c
* @brief: Server Program
* */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define MAX_LINE 256
int conn_count=0;
void *HandlingClient(void *);
int main(int argc,char *argv[])
{
struct sockaddr_in serv_addr, client_addr;
int sockid, newsockid, len, ret;
char buf[MAX_LINE];
if(argc < 2 )
{
printf("usage: %s <port>\n", argv[0]);
return -1;
}
sockid = socket(AF_INET,SOCK_DGRAM, 0);
if(sockid < 0 )
{
printf("Failed to get socket\n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if((bind(sockid,(struct sockaddr*)&serv_addr,sizeof(serv_addr))) != 0)
{
printf("Failed to bind\n");
return -2;
}
len = sizeof(client_addr);
while(1)
{
ret = recvfrom(sockid,&buf,MAX_LINE,0, (struct sockaddr*)&client_addr, &len);
if ( ret == 0 )
{
break;
}
else if ( ret < 0 )
{
printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
break;
}
if(!strncasecmp(buf,"bye",3))
{
break;
}
/* send back to client*/
if( (ret = sendto(sockid, &buf, ret, 0, (struct sockaddr*)&client_addr, sizeof(client_addr))) <= 0 )
{
printf("Client coultn't send message to server\n");
break;
}
}
close(sockid);
return 0;
}
/*
* @filename : client.c
* @brief: Client Program
* */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAX_LINE 256
void *SendReceive(void* sockid);
int main(int argc,char *argv[])
{
struct sockaddr_in serv_addr, cli_addr ;
int sockid;
char buf[MAX_LINE],sendbuf[MAX_LINE];
int length,ret ;
socklen_t len;
if(argc < 3 )
{
printf("usage: %s <server-address> <port> \n",argv[0]);
return -1;
}
sockid = socket(AF_INET,SOCK_DGRAM,0) ;
if(sockid == -1 )
{
printf("Failed to get socket\n");
return -2;
}
serv_addr.sin_family = AF_INET ;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]) ;
serv_addr.sin_port = htons(atoi(argv[2])) ;
len = 1;
while(1)
{
memset(buf,0,sizeof(buf));
if(fgets(buf,MAX_LINE,stdin) == NULL )
{
printf ("received EOF from stdin\n");
break;
}
length = strlen(buf);
if( (ret = sendto(sockid, &buf, length, 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) <= 0 )
{
printf("Client coultn't send message to server\n");
break;
}
ret = recvfrom(sockid,&buf,MAX_LINE,0, (struct sockaddr*)&cli_addr, &len);
if ( ret == 0 )
{
printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
break;
}
else if ( ret < 0 )
{
printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
break;
}
printf("%s",buf);
}
close(sockid);
return 0;
}
MESSAGE="Echo Program Built Successfully"
MESSAGE1 = " ------------------------- "
CC = gcc
CFLAGS = -g -I./include
all: server client
@echo $(MESSAGE1)
@echo $(MESSAGE)
@echo $(MESSAGE1)
server: server.o
${CC} -o $@ server.o
client: client.o
${CC} -o $@ client.o
clean:
-@rm *.o
distclean: clean
-@rm server client
No comments:
Post a Comment