Created
September 21, 2012 15:07
-
-
Save yifu/3762053 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <sys/types.h> | |
| #include <sys/socket.h> | |
| #include <netinet/in.h> | |
| #include <arpa/inet.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| int main() | |
| { | |
| std::cout << "hello world" << std::endl; | |
| struct sockaddr_in stSockAddr; | |
| int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
| if(-1 == SocketFD) | |
| { | |
| perror("can not create socket"); | |
| exit(EXIT_FAILURE); | |
| } | |
| memset(&stSockAddr, 0, sizeof(stSockAddr)); | |
| stSockAddr.sin_family = AF_INET; | |
| stSockAddr.sin_port = htons(1100); | |
| inet_pton(AF_INET, INADDR_ANY, &(stSockAddr.sin_addr.s_addr)); | |
| if(-1 == bind(SocketFD,(struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) | |
| { | |
| perror("error bind failed"); | |
| close(SocketFD); | |
| exit(EXIT_FAILURE); | |
| } | |
| if(-1 == listen(SocketFD, 10)) | |
| { | |
| perror("error listen failed"); | |
| close(SocketFD); | |
| exit(EXIT_FAILURE); | |
| } | |
| for(;;) | |
| { | |
| int ConnectFD = accept(SocketFD, NULL, NULL); | |
| if(0 > ConnectFD) | |
| { | |
| perror("error accept failed"); | |
| close(SocketFD); | |
| exit(EXIT_FAILURE); | |
| } | |
| /* perform read write operations ... | |
| read(sockfd,buff,size)*/ | |
| shutdown(ConnectFD, SHUT_RDWR); | |
| close(ConnectFD); | |
| } | |
| close(SocketFD); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment