- 转载,原文地址:http://blog.csdn.net/liangxiaozhang/article/details/8267854
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <strings.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
-
- int main(){
- int sockfd = socket(AF_INET,SOCK_STREAM,0);
- if(sockfd==-1)perror("sock");
- struct sockaddr_in mine,dest;
-
- bzero(&mine,sizeof(mine));
- mine.sin_family = AF_INET;
- mine.sin_port = htons(3334);
- inet_pton(AF_INET,"192.168.1.100",&mine.sin_addr);
-
- bzero(&dest,sizeof(dest));
- dest.sin_family = AF_INET;
- dest.sin_port = htons(9999);
- inet_pton(AF_INET,"192.168.1.100",&dest.sin_addr);
-
- int b = bind(sockfd,(struct sockaddr*)&mine,sizeof(mine));
- if(b==-1)perror("");
-
- int c = connect(sockfd,(struct sockaddr*)&dest,sizeof(dest));
- if(c!=0)perror("");
-
- close(sockfd);
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <strings.h>
-
- int main(){
- int sockfd = socket(AF_INET,SOCK_STREAM,0);
- struct sockaddr_in addr;
- bzero(&addr,sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons(9999);
- inet_pton(AF_INET,"192.168.1.100",&addr.sin_addr);
-
- bind(sockfd,(struct sockaddr*)&addr,sizeof(addr));
- listen(sockfd,5);
- while(1){
- struct sockaddr_in c_addr;
- int c_len = sizeof(c_addr);
- bzero(&c_addr,c_len);
- int c = accept(sockfd,(struct sockaddr*)&addr,&c_len);
- if(c==-1)perror("");
- char ip[40]={0};
- int port;
- inet_ntop(AF_INET,&addr.sin_addr,ip,40);
- port = ntohs(addr.sin_port);
- printf("from %s:%d\n",ip,port);
- close(c);
-
- }
-
- }