2020-12-03 18:27:05 +01:00
|
|
|
#include <net/socket.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
const char msg1[]="Hello!";
|
|
|
|
char msg2='\0';
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2021-01-05 16:29:30 +01:00
|
|
|
struct sockaddr addr={AF_INET4, PORT(23) , 0, 0};
|
2020-12-03 18:27:05 +01:00
|
|
|
|
|
|
|
int sk1=socket(AF_INET4, SOCK_STREAM, IPPROTO_TCP);
|
2021-01-15 22:48:24 +01:00
|
|
|
printf("Open socket: %d. Error: %d\n",sk1, errno);
|
2020-12-03 18:27:05 +01:00
|
|
|
|
|
|
|
bind(sk1, &addr,sizeof(addr));
|
2021-01-15 22:48:24 +01:00
|
|
|
printf("Socket binding. Error: %d\n", errno);
|
2020-12-03 18:27:05 +01:00
|
|
|
|
|
|
|
listen(sk1, 1);
|
2021-01-15 22:48:24 +01:00
|
|
|
printf("Listening to a socket. Error: %d\n", errno);
|
2020-12-03 18:27:05 +01:00
|
|
|
int sk2 = accept(sk1, &addr, sizeof(addr));
|
2021-01-15 22:48:24 +01:00
|
|
|
printf("Accept done. Error: %d\n", errno);
|
2020-12-03 18:27:05 +01:00
|
|
|
|
|
|
|
send(sk2, msg1, strlen(msg1),MSG_NOFLAG);
|
2021-01-15 22:48:24 +01:00
|
|
|
printf("Send message: '%s' Error: %d\n", msg1, errno);
|
2020-12-03 18:27:05 +01:00
|
|
|
puts("Received data:");
|
|
|
|
while(msg2!='!')
|
|
|
|
{
|
|
|
|
recv(sk2, &msg2, 1, MSG_NOFLAG);
|
|
|
|
printf("%c",msg2);
|
|
|
|
}
|
|
|
|
close(sk1);
|
|
|
|
close(sk2);
|
|
|
|
puts("\nGood bye!");
|
|
|
|
exit(0);
|
|
|
|
}
|