tcpclient
/*#include <errno.h>
#include <string.h>
#include <time.h>
#include <arpa/inet.h>
#include <unistd.h>*/
#include <stdlib.h>
#include<stdio.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]="127.0.0.1";
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,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] Bieu thuc 1^2+2^2+3^2+4^2+..+n^2 (n>=0)
");
printf("[5] Bieu thuc 1^3+2^3+3^3+4^3+..+n^3 (n>=0)
");
printf("[6] Bieu thuc 1^2+3^2+5^2+..+(2n+1)^2 (n>=0)
");
printf("[7] Bieu thuc 2^2+4^2+6^2+..+(2n)^2 (n>=0)
");
printf("Chon bieu thuc can tinh: ");
scanf("%d",&a);
printf("Nhap n: ");
scanf("%d",&b);
send(sockfd, &a, sizeof(a),0);
send(sockfd, &b, sizeof(b), 0);
recv(sockfd, &kq, sizeof(kq),0);
printf("Ket qua = %d
",kq);
//---------------------------------
// Step 5: Close Socket
close(sockfd);
return 0;
}
Bạn đang đọc truyện trên: AzTruyen.Top