赞
踩
头文件:
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <errno.h>
#include<stdio.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <errno.h> #include<stdio.h> #include <netinet/in.h> #include <netinet/ip.h> /* superset of previous */ #include <arpa/inet.h> #include <string.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char const *argv[]) { if (argc != 2) { printf("usage: <port>\n"); return -1; } //1.创建套接字 int sockfd= socket(AF_INET, SOCK_STREAM, 0); if (sockfd <0 ) { perror("socket err."); return -1; } printf("sockfd:%d\n",sockfd); //2.绑定ip+port //填充结构体 struct sockaddr_in saddr,caddr; saddr.sin_family=AF_INET;//协议族 saddr.sin_port=htons(atoi(argv[1]));//端口号 #if 0 saddr.sin_addr.s_addr=inet_addr(argv[1]);//ip #else //绑定电脑上的所有可用Ip给服务器 //saddr.sin_addr.s_addr=INADDR_ANY; saddr.sin_addr.s_addr=inet_addr("0.0.0.0");//ip #endif socklen_t len=sizeof(saddr); if(bind(sockfd, (struct sockaddr *)&saddr,len) <0) { perror("bind err."); return -1; } printf("bind success\n"); //3.监听,将主动套接字变成被动套接字 if(listen(sockfd, 6)<0) { perror("listen err."); return -1; } printf("listen success\n"); //4.accept阻塞等待连接产生,有连接则创建套接字。 while(1) { int acceptfd=accept(sockfd, (struct sockaddr *)&caddr, &len); if (acceptfd<0) { perror("accept err."); return -1; } printf("connect ok\n"); printf("acceptfd:%d\n",acceptfd); printf("ip:%s port:%d\n",inet_ntoa(caddr.sin_addr),ntohs(caddr.sin_port)); //5.发送 #define N 64 char buf[N]={}; while (1) { int ret=recv(acceptfd,buf, N, 0);//返回值是实际读到的数量 //ret的返回值有讲究, //当ret>0时代表对方发数据了,当ret==0时,代表对端退出,ret<0时代表接收出错 if (ret < 0) { perror("recv err."); return -1; }else if (ret > 0) { printf("buf:%s\n",buf); memset(buf,0,N); }else { printf("client exit\n"); close(acceptfd); break; } } } //6.关闭套接字 close(sockfd); return 0; } }
创建套接字socket-发送连接请求connect-发送数据(gets-send-memset)-关闭套接字close
- #include <sys/types.h> /* See NOTES */
- #include <sys/socket.h>
- #include <errno.h>
- #include<stdio.h>
- #include <netinet/in.h>
- #include <netinet/ip.h> /* superset of previous */
- #include <arpa/inet.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
-
- int main(int argc, char const *argv[])
- {
- if (argc != 3)
- {
- printf("usage:<ip> <port>\n");
- return -1;
- }
-
- //1.创建套接字
- int sockfd= socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd <0 )
- {
- perror("socket err.");
- return -1;
- }
- printf("sockfd:%d\n",sockfd);
- //2.发送连接请求
- struct sockaddr_in addr;
- addr.sin_family=AF_INET;
- addr.sin_port=htons(atoi(argv[2]));
- addr.sin_addr.s_addr=inet_addr(argv[1]);
- socklen_t addrlen=sizeof(addr);
- if(connect(sockfd, (struct sockaddr *)&addr,addrlen)<0)
- {
- perror("connect err.");
- return -1;
- }
- //3.发送数据
- #define N 64
- char buf[N]={0};
- while (1)
- {
- gets(buf);
- send(sockfd,buf,N,0);
- memset(buf,0,N);
- }
-
- //4.关闭套接字
- close(sockfd);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。