working udp
This commit is contained in:
@@ -4,7 +4,7 @@ int main(int argc, char *argv[]) {
|
||||
// char quote[1024];
|
||||
// getRandomQuote(quote);
|
||||
// printf("%s\n", quote);
|
||||
tcpServer();
|
||||
udpServer();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -4,10 +4,18 @@
|
||||
|
||||
#include "server.h"
|
||||
|
||||
//TODO: refactor
|
||||
//TODO: object-like C
|
||||
|
||||
extern int errno;
|
||||
|
||||
unsigned long addressLength = sizeof(struct sockaddr_in);
|
||||
|
||||
|
||||
void tcpServer() {
|
||||
int serverSocket, clientSocket;
|
||||
long read_size;
|
||||
unsigned long addressLength;
|
||||
long recvLen;
|
||||
// unsigned long addressLength = sizeof(struct sockaddr_in);
|
||||
struct sockaddr_in serverAddress, clientAddress;
|
||||
char quote[MESSAGE_STRING_LENGTH] = "";
|
||||
char clientMessage[MESSAGE_STRING_LENGTH] = "";
|
||||
@@ -38,13 +46,13 @@ void tcpServer() {
|
||||
|
||||
/*accept connections*/
|
||||
//TODO: t h r e a d s
|
||||
addressLength = sizeof(struct sockaddr_in);
|
||||
while ((clientSocket = accept(serverSocket, (struct sockaddr *) &clientAddress, (socklen_t *) &addressLength)) >= 0) {
|
||||
while ((clientSocket = accept(serverSocket, (struct sockaddr *) &clientAddress, (socklen_t *) &addressLength)) >=
|
||||
0) {
|
||||
puts("client connected");
|
||||
|
||||
/*receive data*/
|
||||
while ((read_size = recv(clientSocket, clientMessage, MESSAGE_STRING_LENGTH, 0))) {
|
||||
if (read_size < 0) {
|
||||
while ((recvLen = recv(clientSocket, clientMessage, MESSAGE_STRING_LENGTH, 0))) {
|
||||
if (recvLen < 0) {
|
||||
puts("receive failed");
|
||||
} else {
|
||||
printf("client says: %s\n", clientMessage);
|
||||
@@ -52,7 +60,7 @@ void tcpServer() {
|
||||
/*send data*/
|
||||
getRandomQuote(quote);
|
||||
printf("responding: \"%s\"\n", quote);
|
||||
write(clientSocket, quote, strlen(quote));
|
||||
send(clientSocket, quote, strlen(quote), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +73,57 @@ void tcpServer() {
|
||||
}
|
||||
}
|
||||
|
||||
void udpServer() {
|
||||
//TODO: udp handling
|
||||
_Noreturn void udpServer() {
|
||||
int sock;
|
||||
// unsigned long addressLength = sizeof(struct sockaddr_in);
|
||||
struct sockaddr_in serverAddress, clientAddress;
|
||||
char quote[MESSAGE_STRING_LENGTH] = "";
|
||||
char clientMessage[MESSAGE_STRING_LENGTH] = "";
|
||||
|
||||
/*initialize sock info*/
|
||||
memset(&serverAddress, 0, addressLength);
|
||||
memset(&clientAddress, 0, addressLength);
|
||||
|
||||
serverAddress.sin_family = AF_INET;
|
||||
serverAddress.sin_addr.s_addr = INADDR_ANY;
|
||||
serverAddress.sin_port = htons(17);
|
||||
|
||||
/*create sock*/
|
||||
sock = socket(serverAddress.sin_family, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (sock == -1) {
|
||||
puts("sock not created");
|
||||
exit(1);
|
||||
}
|
||||
puts("sock created");
|
||||
|
||||
/*bind sock to address:port*/
|
||||
if (bind(sock, (const struct sockaddr *) &serverAddress, sizeof serverAddress) < 0) {
|
||||
puts("bind failed");
|
||||
exit(2);
|
||||
}
|
||||
puts("address bound");
|
||||
|
||||
/*accept connections*/
|
||||
//TODO: t h r e a d s ?
|
||||
while (1) {
|
||||
/*receive data*/
|
||||
int recvLen = recvfrom(sock, clientMessage, MESSAGE_STRING_LENGTH, 0,
|
||||
(struct sockaddr *) &clientAddress, (socklen_t *) &addressLength);
|
||||
|
||||
uint16_t port = ntohs(clientAddress.sin_port);
|
||||
char *ipv4 = inet_ntoa(clientAddress.sin_addr);
|
||||
|
||||
printf("recvLen = %d from = %s:%d\nmsg = `%s`\n", recvLen, ipv4, port, clientMessage);
|
||||
|
||||
perror("recvfrom");
|
||||
if (recvLen >= 0) {
|
||||
printf("client says: %s\n", clientMessage);
|
||||
|
||||
/*send data*/
|
||||
getRandomQuote(quote);
|
||||
printf("responding: `%s`\n", quote);
|
||||
sendto(sock, strcat(quote, "\n"), sizeof quote, 0, (struct sockaddr *) &clientAddress, addressLength);
|
||||
perror("sendto");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,14 @@
|
||||
#define QOTD_SERVER_H
|
||||
|
||||
#include <sys/socket.h>
|
||||
//#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "quote.h"
|
||||
|
||||
@@ -18,6 +21,6 @@
|
||||
|
||||
void tcpServer();
|
||||
|
||||
void udpServer();
|
||||
_Noreturn void udpServer();
|
||||
|
||||
#endif //QOTD_SERVER_H
|
||||
|
||||
Reference in New Issue
Block a user