Created
January 12, 2017 20:37
-
-
Save takahub1/4e1131a6feb9216434c4781c45ba917c to your computer and use it in GitHub Desktop.
This file contains 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 <unistd.h> | |
#include <stdio.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <netdb.h> | |
int main(){ | |
int sock0; | |
struct sockaddr_in addr; | |
struct sockaddr_in client; | |
unsigned int len; | |
int sock; | |
sock0 = socket(AF_INET, SOCK_STREAM, 0); | |
addr.sin_family = AF_INET; | |
addr.sin_port = htons(12345); | |
addr.sin_addr.s_addr = INADDR_ANY; | |
bind(sock0, (struct sockaddr *)&addr, sizeof(addr)); | |
listen(sock0, 5); | |
std::cout<<"listen\r\n"<<std::endl; | |
len = sizeof(client); | |
sock = accept(sock0, (struct sockaddr *)&client, &len); | |
std::cout<<"accepted\r\n"<<std::endl; | |
int i=0; | |
while(1){ | |
if(i==6){ | |
write(sock, "9", 5); | |
i=0; | |
} | |
else if(i%2==0) write(sock, "0", 5); | |
else if(i%2==1) write(sock, "1", 5); | |
std::cout<<"write\r\n"<<std::endl; | |
sleep(1); | |
i++; | |
} | |
close(sock); | |
close(sock0); | |
std::cout<<"closed\r\n"<<std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment