Witam ,próbuję utworzyć prosty echo serwer dla komunikacji TCP, oto kod :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUFLEN 512 //Max length of buffer
#define PORT 8888 //The port on which to listen for incoming data
void error(char* s)
{
perror(s); // define what to do when there is an error
exit(1); // exit the main() function
}
int main(void)
{
struct sockaddr_in si_me, si_other; // creates structures
int s, i, slen = sizeof(si_other), recv_len; // creates ints
char buf[BUFLEN];
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
error("socket"); // if socket() return -1 -- error
}
// zero out the structure
memset((char*)&si_me, 0, sizeof(si_me)); // put zeros into structure
si_me.sin_family = AF_INET; // specify address family (IPv4)
si_me.sin_port = htons(PORT); // specify port
si_me.sin_addr.s_addr = htonl(INADDR_ANY); //binds to all ip addresses
//bind socket to port
if (bind(s, (struct sockaddr*)&si_me, sizeof(si_me)) == -1) {
error("bind"); // if bind() return -1 -- error
}
//keep listening for data
while (1) // inf loop
{
printf("Waiting for data..."); // show info regarding state
fflush(stdout); // clears the buffer
//try to receive some data, this is a blocking call
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr*)&si_other, &slen)) == -1) {
error("recvfrom()"); // if receiving failed -- error
}
//print details of the client/peer and the data received
printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
printf("Data: %s\n", buf); //now reply the client with the same data if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == -1) // &si_other saves data from where packet was read
{
error("sendto()"); // if sending returned -1 -- error
}
}
close(s);
return 0;
}
Następnie kompiluje program i odpalam go w 1 terminalu. Włączam drugi terminal wpisuję komendę ncat –vv localhost 8888 -u
i pokazuje się komunikat:
Ncat: Could not resolve hostname "–vv": Name or service not known. QUITTING.
Nie wiem co może być przyczyną.