#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#define port 8081
#define maxn 100
int main(int argc,char *argv[])
{
int s;
struct sockaddr_in server;
s=socket(AF_INET, SOCK_STREAM, 0);
if(s < 0){
printf("socket error\n");
return -1;
}
else printf("yes\n");
bzero(&server,sizeof (server));
server.sin_family=AF_INET;
server.sin_addr.s_addr = inet_addr("125.216.250.4");
server.sin_port=htons(port);
int err=connect(s, (struct sockaddr*)&server, sizeof (struct sockaddr));
if(err<0)
printf("connect fail\n");
else puts("success");
char buf1[19]="nihao";
char buf2[1024];
ssize_t size=0;
//for(;;){
write(s,buf1,19);
size=read(s,buf2,1024);
if(size>0)
{
printf("read success\n");
puts(buf2);
}
//}
}
socket客户端代码
最新推荐文章于 2022-10-10 01:45:49 发布