Server
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
/**
* ham doc data tu clien va upper string ky tu
*/
void read_from_client(int mysock)
{
char buff[1024];
memset(buff, 0, sizeof(buff));
int rval;
if((rval = recv(mysock, buff, sizeof(buff), 0))<0)
perror("doc stream that bai");
else
if(rval==0)
printf("Ending connection\n");
else
{
printf("tin nhan duoc: %s\n", buff);
int n = strlen(buff),i;
for(i=0;i<n;i++)
{
buff[i] = toupper(buff[i]);
}
printf("tin gui tro lai: %s\n", buff);
}
printf("got the message (rval = %d)\n",rval);
if(send(mysock,buff,sizeof(buff),0)<0)
{
perror("gui nguoc that bai");
close(mysock);
exit(1);
}
}
//ham tao ket noi
int make_new_connection(int sock)
{
int mysock;
mysock = accept(sock,(struct sockaddr *)0,0);
return mysock;
}
/**
* ham tao socket
*/
int make_socket()
{
int sock;
struct sockaddr_in server;
/*Tao socket*/
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock<0){
perror("Tao socket that bai !");
exit(1);
}
/*Tao thong tin cho socket*/
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(5000);
/*tai su dung port*/
int reuse =1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (int *) &reuse, sizeof(reuse));
/*Call bind*/
if(bind(sock, (struct sockaddr *)&server, sizeof(server)))
{
perror("bind that bai");
exit(1);
}
return sock;
}
int main(int argc, char *argv[])
{
/*variables*/
int sock ;
int mysock;
sock = make_socket();
/*listen*/
listen(sock, 5);
/*accept*/
do{
if((mysock = make_new_connection(sock))<0)
{
perror("ket noi that bai");
continue;
}
else
{
read_from_client(mysock);
close(mysock);
}
}while(1);
return 0;
}
Client
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int make_socket(char *addr)
{
int sock;
struct sockaddr_in server;
struct hostent *hp;
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock<0)
{
perror("tao socket that bai");
exit(1);
}
server.sin_family = AF_INET;
/*lay thong tin host*/
hp = gethostbyname(addr);
if(hp==0)
{
perror("gethostbyname failed");
close(sock);
exit(1);
}
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_port = htons(5000);
/*ket noi server*/
if(connect(sock, (struct sockaddr *)&server, sizeof(server))<0)
{
perror("ket noi that bai");
exit(1);
}
return sock;
}
void gui_nhan_data(int sock)
{
char buff[1024];
int rval;
char *DATA = NULL;
size_t thesize;
/*nhap du lieu gui*/
while(DATA == NULL)
{
printf("nhap:");
getline(&DATA,&thesize,stdin);
printf("your input is: %s\n", DATA);
printf("size = %d, sizeof = %d\n", thesize,strlen(DATA));
}
/*send data*/
if(send(sock,DATA,strlen(DATA), 0)<0)
{
perror("gui that bai");
close(sock);
exit(1);
}
printf("tin da gui: %s\n", DATA);
/*receive data*/
memset(buff, 0, sizeof(buff));
if((rval = recv(sock,buff,sizeof(buff),0))<0)
perror("doc lai that bai");
else
if(rval==0)
{
printf("khong co chuoi nao duoc gui");
}
else
{
printf("tin nhan lai: %s\n", buff);
}
}
int main(int argc, char *argv[])
{
/*khai bao bien*/
int sock;
if(argc < 2)
{
printf("chua nhap may chu\n");
return(EXIT_FAILURE);
}
if((sock = make_socket(argv[1]))<0)
{
printf("tao ket noi that bai !");
}
else
{
gui_nhan_data(sock);
}
close(sock);
return 0;
}