User:Viendongshop


 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 
 * 5) include 
 * 6) include 
 * 7) include 
 * 8) include 

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; }