Add size_t ssize_t and socklen_t
This commit is contained in:
19
socktest.c
19
socktest.c
@@ -5,7 +5,6 @@
|
||||
int main() {
|
||||
char recvBuff[4096];
|
||||
int socket_fd, connection_fd;
|
||||
int c;
|
||||
struct sockaddr_in server, client;
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_addr.s_addr = INADDR_ANY;
|
||||
@@ -17,7 +16,7 @@ int main() {
|
||||
if (socket_fd < 0) {
|
||||
write(STDERR, "Socket error\n", 13);
|
||||
close(socket_fd);
|
||||
return(1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Set socket options
|
||||
@@ -25,14 +24,14 @@ int main() {
|
||||
if(0 > setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
|
||||
write(STDERR, "Socket error\n", 13);
|
||||
close(socket_fd);
|
||||
return(2);
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Bind the socket
|
||||
if( bind(socket_fd, (struct sockaddr *)&server, sizeof(server)) < 0) {
|
||||
write(STDERR, "Socket error\n", 13);
|
||||
close(socket_fd);
|
||||
return(3);
|
||||
return 3;
|
||||
}
|
||||
|
||||
// Listen for incoming connections
|
||||
@@ -42,14 +41,14 @@ int main() {
|
||||
write(STDOUT, "Waiting for incoming connections...\n", 36);
|
||||
|
||||
// Get the new connection
|
||||
c = sizeof(struct sockaddr_in);
|
||||
connection_fd = accept(socket_fd, (void *)&client, (uint64_t*)&c);
|
||||
socklen_t c = sizeof(struct sockaddr_in);
|
||||
connection_fd = accept(socket_fd, (void *)&client, &c);
|
||||
write(STDOUT, "Got new connection\n", 19);
|
||||
if(connection_fd < 0) {
|
||||
write(STDERR, "Connection error\n", 17);
|
||||
close(connection_fd);
|
||||
close(socket_fd);
|
||||
return(5);
|
||||
return 5;
|
||||
}
|
||||
|
||||
|
||||
@@ -63,18 +62,18 @@ int main() {
|
||||
write(STDERR, "Connection error\n", 17);
|
||||
close(connection_fd);
|
||||
close(socket_fd);
|
||||
return(6);
|
||||
return 6;
|
||||
}
|
||||
|
||||
// Recieve stuff
|
||||
write(STDOUT, "Recieving data...\n", 18);
|
||||
for(;;) {
|
||||
int64_t incoming = recv(connection_fd, recvBuff, 4096, 0);
|
||||
ssize_t incoming = recv(connection_fd, recvBuff, 4096, 0);
|
||||
if( incoming < 0) {
|
||||
write(STDERR, "Connection error\n", 17);
|
||||
close(connection_fd);
|
||||
close(socket_fd);
|
||||
return(7);
|
||||
return 7;
|
||||
}if(incoming == 0) break;
|
||||
|
||||
// Print the recieved data
|
||||
|
||||
Reference in New Issue
Block a user