USCclient
#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
/* TCP Tinh cac bieu thuc (client)*/
void error(char *msg)
{
printf("%s
",msg);
exit(1);
}
int main()
{
char serverIP[20]="192.168.1.2";// Sua
int portno=12345;
int sockfd;
struct sockaddr_in serv_addr;
char buffer[256];
int n,solan;
//solan = 0;
//-------------------------------------
//while (solan < 3)
//{
//------------------------------
// Step 1: Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("ERROR opening socket");
//-------------------------------
// Step 2: Prepair Server Address Struct
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr=inet_addr(serverIP);
serv_addr.sin_port = htons(portno);
// Step 3: Connect to Server
if (connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Connected to Server!
");
//-------------------------------
// Step 4: Send/Receive Data
printf("Bai5: TCP Tinh cac bieu thuc
");
printf("Day la client
");
int a,b,c,kq;
printf("[1] Tong 1+3+5+7+....+(2n+1)
");
printf("[2] Tong 1*2 + 2*3+...+n*(n+1)
");
printf("[3] Bieu thuc 1-2+3-4+..+(2n+1)
");
printf("[4] USCLN
");
printf("Chon bieu thuc can tinh: ");
scanf("%d",&a);
printf("Nhap so thu nhat: ");
scanf("%d",&b);
printf("Nhap so thu hai: ");
scanf("%d",&c);
send(sockfd, &a, sizeof(a),0);
send(sockfd, &b, sizeof(b), 0);
send(sockfd, &c, sizeof(c), 0);
recv(sockfd, &kq, sizeof(kq),0);
printf("Ket qua = %d
",kq);
// solan = solan + 1;
//}
//---------------------------------
// Step 5: Close Socket
close(sockfd);
return 0;
}
Bạn đang đọc truyện trên: Truyen247.Pro