标签:
地址转换函数:
1.将字符串的IP地址转换成网络字节序的地址
int connect(int sock, const struct sockaddr *addr, socklen_t len);
功能描述:建立从客户端到服务器的连接 返回:成功返回0,失败返回-1
5 close() int close(int s); 功能 关闭套接字。
参数说明: s – 要关闭的套接字
1 #include <unistd.h> 2 #include <stdio.h> 3 #include <sys/socket.h> 4 #include <netinet/in.h> 5 #include <arpa/inet.h> 6 #include <pthread.h> 7 8 int main() 9 { 10 int sock,size; 11 uint16_t port = 8000; 12 const char *IP = "192.168.7.118"; 13 char buf[1024] = "songlongfei"; 14 struct sockaddr_in addr; 15 memset(&addr,0,sizeof(addr)); 16 sock = socket(AF_INET,SOCK_STREAM,0); 17 addr.sin_family = AF_INET; 18 addr.sin_port = htons(port); 19 inet_pton(AF_INET,IP,&addr.sin_addr); 20 if(connect(sock,(struct sockaddr*)&addr,sizeof(addr)) != 0) 21 printf("Connect failed\n"); 22 while(1) 23 { 24 fgets(buf,sizeof(buf),stdin); 25 if((size = send(sock,buf,sizeof(buf),0)) < 0) 26 printf("send failed\n"); 27 else 28 printf("Connect succ!\n"); 29 recv(sock,buf,sizeof(buf),0); 30 printf("%s\n",buf); 31 } 32 close(sock); 33 }
标签:
原文地址:http://www.cnblogs.com/be-m/p/4340075.html