use ggg
This commit is contained in:
parent
c0eea479aa
commit
d8c2866895
41
ccc.c
41
ccc.c
@ -13,46 +13,37 @@
|
|||||||
|
|
||||||
|
|
||||||
int main(int argc, const char * argv[]) {
|
int main(int argc, const char * argv[]) {
|
||||||
// insert code here...
|
|
||||||
printf("Hello, World!\n");
|
|
||||||
|
|
||||||
int client_sockfd;
|
int client_sockfd;
|
||||||
int len;
|
int len;
|
||||||
struct sockaddr_in remota_addr; //服务器端网络地址结构体
|
struct sockaddr_in remota_addr;
|
||||||
char buf[BUFSIZ]; //数据传送的缓冲区
|
char buf[BUFSIZ];
|
||||||
memset(&remota_addr,0,sizeof(remota_addr)); //数据初始化--清零
|
|
||||||
remota_addr.sin_family = AF_INET; //设置为IPV4通信
|
memset(&remota_addr,0,sizeof(remota_addr));
|
||||||
|
remota_addr.sin_family = AF_INET;
|
||||||
remota_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
remota_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||||
remota_addr.sin_port = htons(8000); //服务器端口号
|
remota_addr.sin_port = htons(8000);
|
||||||
|
|
||||||
//创建客户端套接字--Ipv4协议,面向连接通信,TCP协议
|
|
||||||
//成功,返回0 ,失败返回-1
|
|
||||||
if ((client_sockfd=socket(PF_INET, SOCK_STREAM, 0))<0) {
|
|
||||||
perror("socket");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
//将套接字绑定到服务器的网络地址上
|
|
||||||
if (connect(client_sockfd, (struct sockaddr *)&remota_addr, sizeof(struct sockaddr))<0) {
|
|
||||||
perror("connect");
|
client_sockfd=socket(PF_INET, SOCK_STREAM, 0);
|
||||||
return 1;
|
|
||||||
}
|
connect(client_sockfd, (struct sockaddr *)&remota_addr, sizeof(struct sockaddr));
|
||||||
|
|
||||||
printf("connect to server\n");
|
printf("connect to server\n");
|
||||||
|
|
||||||
len = recv(client_sockfd, buf, BUFSIZ, 0); //接受服务器端消息
|
len = recv(client_sockfd, buf, BUFSIZ, 0);
|
||||||
buf[len]='/0';
|
buf[len]='/0';
|
||||||
printf("%s",buf); //打印服务器端消息
|
printf("%s",buf);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//循环的发送信息并打印接受消息--recv返回接收到的字节数,send返回发送的字节数
|
|
||||||
while (1) {
|
while (1) {
|
||||||
memset(buf,0,sizeof(buf));
|
memset(buf,0,sizeof(buf));
|
||||||
|
|
||||||
printf("Enter string to send");
|
printf("Enter string to send");
|
||||||
scanf("%s",buf);
|
scanf("%s",buf);
|
||||||
// if (!strcmp(buf,"quit")) {
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
|
|
||||||
len=send(client_sockfd,buf,strlen(buf),0);
|
len=send(client_sockfd,buf,strlen(buf),0);
|
||||||
memset(buf,0,sizeof(buf));
|
memset(buf,0,sizeof(buf));
|
||||||
@ -65,9 +56,7 @@ int main(int argc, const char * argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
close(client_sockfd);
|
close(client_sockfd);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user