#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#if 0
#define UDP
#else
#define TCP
#endif
int sockfd;
char* IP = "10.22.35.102";
//char *IP = "127.0.0.1";
#ifdef UDP
short PORT = 1025;
#endif
#ifdef TCP
short PORT = 2222;
#endif
typedef struct sockaddr SA;
void init(){
#ifdef UDP
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
#endif
#ifdef TCP
sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = inet_addr(IP);
if(connect(sockfd, (SA*)&addr, sizeof(addr)) == -1){
printf("connect failed!\n");
exit(-1);
}
#endif
}
int main (int argc, char *argv[]) {
sockfd = -1;
//char buf[2048];
char buf[512];
int index, i = 0;
if (argc == 2) {
index = atoi(argv[1]);
} else {
printf("Usage: ./client <times>\n");
return -1;
}
printf("start init ....\n");
init();
printf("connected...\n");
memset(buf, 1, sizeof(buf));
#ifdef UDP
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = inet_addr(IP);
#endif
printf("<mayue> 01 sockfd:%d\n", sockfd);
while (1) {
//printf("please input something:\n");
//scanf("%s", buf);
//puts(buf);
if (i == index)
break;
#ifdef TCP
send(sockfd, buf, sizeof(buf), 0);
printf("send 2048! index:%d\n", i);
#endif
#ifdef UDP
printf("sendto 2048! index:%d\n", i);
if (sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1){
printf("sendto error!\n");
return -1;
}
#endif
//sleep(1);
i++;
}
printf("<mayue> 01 sockfd:%d\n", sockfd);
int ret;
//ret = shutdown(sockfd,SHUT_RDWR);
printf("<mayue> ret:%d\n", ret);
while(1)
{
sleep(1);
}
close(sockfd);
return 0;
}
C socket client
最新推荐文章于 2023-05-11 17:17:11 发布