Я нуб, пытающийся установить sh простое UDP-соединение между клиентом и сервером с использованием Ipv6 и C -Language.
По-видимому, функция sendto () возвращает 1 (ошибка ), но, если честно, я совсем не понимаю этого.
Если кто-то захочет взглянуть на это и помочь, то это будет очень ценно!
спасибо. alex
//Server
#define _CRT_SECURE_NO_WARNINGS
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <in6addr.h>
#include <ws2ipdef.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <WS2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
int startWinsock(void)
{
WSADATA wsa;
return WSAStartup(MAKEWORD(2, 0), &wsa);
}
#define IN6AADR_ANY_INIT {{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}}
struct in6_addr anyaddr = IN6AADR_ANY_INIT;
int main()
{
printf("this is the Server. \n \n");
long rc;
int socket_fd;
char buf[256];
char buf2[300];
struct sockaddr_in6 remoteAddr;
int remoteAddrLen = sizeof(struct sockaddr_in6);
struct sockaddr_in6 server_addr, client_addr;
rc = startWinsock();
if (rc != 0)
{
printf("error: startWinsock, error code: %d\n", rc);
return 1;
}
else
{
printf("Winsock started!\n");
}
//UDP Socket creation
socket_fd = socket(AF_INET6, SOCK_DGRAM, 0);
if (socket_fd == INVALID_SOCKET)
{
printf("error creating socket, error code: %d\n", WSAGetLastError());
return 1;
}
else
{
printf("UDP Socket erstellt!\n");
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin6_family = AF_INET6;
server_addr.sin6_addr = in6addr_any;
server_addr.sin6_port = htons(50000);
rc = bind(socket_fd, (struct sockaddr *) &server_addr, sizeof(server_addr));
if (rc == SOCKET_ERROR)
{
printf("error: bind, error code: %d\n", WSAGetLastError());
return 1;
}
else
{
printf("Socket bind to Port 50.000 \n");
}
while (1)
{
socklen_t clilen = sizeof(client_addr);
rc = recvfrom(socket_fd, buf, 256, 0, (struct sockaddr *) &client_addr, &clilen);
if (rc == SOCKET_ERROR)
{
printf("Error: recvfrom, error code: %d\n", WSAGetLastError());
return 1;
}
else
{
printf("%d Bytes received!\n", rc);
buf[rc] = '\0';
}
printf("recieved data: %s\n", buf);
//Answer
sprintf(buf2, "Du mich auch %s", buf);
rc = sendto(socket_fd, buf2, strlen(buf2), 0, (struct sockaddr_in6*)&remoteAddr, remoteAddrLen);
if (rc == SOCKET_ERROR)
{
printf("Error: sendto, error code: %d\n", WSAGetLastError());
return 1;
}
else
{
printf("%d Bytes sent!\n", rc);
}
}
return 0;
}
//Client
#define _CRT_SECURE_NO_WARNINGS
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <in6addr.h>
#include <ws2ipdef.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <WS2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#define SERVADDR "127.0.0.1"
//"fe80::e7:edbf:1df6:7451%5"
int startWinsock(void)
{
WSADATA wsa;
return WSAStartup(MAKEWORD(2, 0), &wsa);
}
int main()
{
printf("this is the client. \n \n");
long rc;
int socket_fd;
socklen_t clilen;
struct sockaddr_in6 server_addr, client_addr;
char buf[256];
char addrbuf[INET6_ADDRSTRLEN];
rc = startWinsock();
if (rc != 0)
{
printf("error: startWinsock, error code: %d\n", rc);
return 1;
}
else
{
printf("Winsock started!\n");
}
//UDP Socket creation
socket_fd = socket(AF_INET6, SOCK_DGRAM, 0);
if (socket_fd == INVALID_SOCKET)
{
printf("error creating socket, error code: %d\n", WSAGetLastError());
return 1;
}
else
{
printf("UDP Socket created!\n");
}
memset(&server_addr, 0, sizeof(server_addr));
// addr prepatation
server_addr.sin6_family = AF_INET6;
inet_pton(AF_INET6, SERVADDR, &server_addr.sin6_addr);
server_addr.sin6_port = htons(50000);
while (1)
{
printf("insert text: ");
gets(buf);
rc = sendto(socket_fd, buf, strlen(buf), 0, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_in6));
if (rc == SOCKET_ERROR)
{
printf("error: sendto, error code: %d\n", WSAGetLastError());
return 1;
}
else
{
printf("%d Bytes sent!\n", rc);
}
rc = recvfrom(socket_fd, buf, 256, 0, (struct sockaddr *)&client_addr, &clilen);
if (rc == SOCKET_ERROR)
{
printf("error: recvfrom, error code: %d\n", WSAGetLastError());
return 1;
}
else
{
printf("%d Bytes received!\n", rc);
buf[rc] = '\0';
printf("received data: %s\n", buf);
}
}
return 0;
}