created basic server & client code

Oh, did I say "created"? I meant stole :)
This commit is contained in:
Václav Šmejkal 2023-02-09 17:04:11 +01:00
parent e068e62c48
commit a68a11d371
Signed by: ENGO150
GPG Key ID: F6D6DF86242C5A59
2 changed files with 62 additions and 3 deletions

View File

@ -1,8 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
int main(void)
#include <netinet/in.h>
int main()
{
printf("Here will be client...\n");
int sockD = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in servAddr;
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(9001);
servAddr.sin_addr.s_addr = INADDR_ANY;
int connectStatus = connect(sockD, (struct sockaddr*) &servAddr, sizeof(servAddr));
if (connectStatus == -1)
{
printf("Error...\n");
} else
{
char strData[255];
recv(sockD, strData, sizeof(strData), 0);
send(sockD, "NE", 3, 0);
printf("Message: %s\n", strData);
}
return 0;
}

View File

@ -1,8 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
int main(void)
{
printf("Here will be server...\n");
int servSockD = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET
//TODO: REMOVE
char serMsg[255] = "Message from the server to the "
"client \'Hello Client\' ";
//DEFINE SERVER ADDRESS
struct sockaddr_in servAddr;
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(9001);
servAddr.sin_addr.s_addr = INADDR_ANY;
//BIND SOCKET
bind(servSockD, (struct sockaddr*) &servAddr, sizeof(servAddr));
//LISTEN
listen(servSockD, 1);
//CLIENT SOCKET
int clientSocket = accept(servSockD, NULL, NULL);
char clientMsg[255];
//SEND
send(clientSocket, serMsg, sizeof(serMsg), 0);
recv(clientSocket, clientMsg, sizeof(clientMsg), 0);
printf("%s\n", clientMsg);
return 0;
}