Winsock UDP filesending error 10040 - c++

This is my sending programm.
#pragma once
#pragma comment(lib,"Ws2_32.lib")
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
WSAData wsaData;
WORD DllVersion = MAKEWORD(2,2);
int startup_RetVal = WSAStartup(DllVersion, &wsaData);
SOCKET sSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
SOCKADDR_IN addr;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_family = AF_INET;
addr.sin_port = htons(22222);
char buf[200000] = "AR*REF=";
int send_RetVal = sendto(sSocket, buf, 200000, NULL, (SOCKADDR*)&addr, sizeof(addr));
if(send_RetVal == SOCKET_ERROR)
{
cout <<" An error occured " << WSAGetLastError() << endl;
getchar();
}
return 0;
}
I get a WSAEMSGSIZE (10040) error.
The goal is to send a 100Kbytes file over udp. I was told that similar error on .NET was solved this way :
IPHostEntry^ IPHostTV;
IPEndPoint^ send_tv_ip;
Socket^ UDPSendTV;
int PortSendTV;
System::String^ IPSend;
send_tv_ip =
gcnew IPEndPoint(IPHostTV->AddressList[0], PortSendTV);
UDPSendTV =
gcnew Socket(send_tv_ip->Address->AddressFamily, SocketType::Dgram, ProtocolType::Udp);
//Increasing buffer and timeout
UDPSendTV->SendTimeout = 1000;
UDPSendTV->SendBufferSize = 100000;
UDPSendTV->SendTo(buff1, 0, size1, SocketFlags::None, send_tv_ip);
How do I modify my sockets so they work correctly?

The message size over UDP is limited by the protocol to ~64KB by the 16-bit message size field in the UDP header. There is no workaround.
(well, except for sending multiple messages per protocol unit).

Related

i don't know why a very basic code related to socket is causing problem

It's a socket communication code that I've been using for a long time, but a few days ago, I tried to wrap it up but it didn't work at all. I checked setting of the port number of inbound rules but it doesn't work. so I'm asking you this question
If setting ip to 127.0.0.1 and run each two files on the same pc, there will be no communication problem. But the problem is that if I run the server and the client file on the two different networks, I get 10060 error
I checked the public ip of each pc several times and checked the port setting several times in the inbound rule. And I thought it was an individual problem with the PC, so I tried it with 6 different PCs, but the results were the same. It's a very basic code, but I don't know which part has a problem because there is an error.
this is my server code and client code
Server
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <WinSock2.h>
#pragma comment(lib,"ws2_32")
#define PORT 10168
#define PACKET_SIZE 1024
using namespace std;
void ConnectSocket();
char* ClientIndex;
SOCKET hc = NULL;
SOCKET hListen;
SOCKADDR_IN tListenAddr = {};
int main()
{
ClientIndex = (char*)malloc(sizeof(char) * 128);
ClientIndex = (char*)"123.456.789.123";
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
hListen = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
tListenAddr.sin_family = AF_INET;
tListenAddr.sin_port = htons(PORT);
tListenAddr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(hListen, (SOCKADDR*)&tListenAddr, sizeof(tListenAddr));
listen(hListen, 20);
for (;;)
{
ConnectSocket();
if (hc != NULL)
{
cout << "success" << endl;
break;
}
Sleep(200);
}
}
void ConnectSocket()
{
SOCKADDR_IN tCIntAddr = {};
int iCIntSize = sizeof(tCIntAddr);
SOCKET hclient = accept(hListen, (SOCKADDR*)&tCIntAddr, &iCIntSize);
if (!strcmp(inet_ntoa(tCIntAddr.sin_addr), ClientIndex))
{
hc = hclient;
}
hclient = NULL;
return;
}
Client
#include "stdio.h"
#include <iostream>
#include <WinSock2.h>
#include "windows.h"
#include <ws2tcpip.h>
#pragma comment(lib,"ws2_32")
#define PORT 10168
#define PACKET_SIZE 1024
#define SERVER_IP "123.456.123.456"
using namespace std;
int main()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET hSocket;
hSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN tAddr = {};
tAddr.sin_family = AF_INET;
tAddr.sin_port = htons(PORT);
tAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
if (connect(hSocket, (sockaddr*)&tAddr, sizeof(tAddr)) == -1)
cout << "connect fail : " << GetLastError() << endl;
}
I solve it.
problem is port forwarding setting of router

I can only receive one word (winsock c++ tcp/ip server and client)

I managed to create a tcp server and client with winsock in c++ but for a reason that is unknown to me I can not send more than one word at a time.
see by yourself:
I specify that I am an absolute beginner and that my code probably contains errors so do not hesitate to correct me if you find some. (also english isn't my first language so sorry for the spelling mistake).
I ask for help because I just spent several hours looking for a solution to my problem without finding it. I think I missed part of my code but I do not know where and why. I looked at the server and client source code created by other people but I do not find how my own is different. (I am sorry if I did a very simple mistake)
client.cpp
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <string>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
std::cout << "--- Chat Section ---" << std::endl;
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
sockaddr_in dest;
dest.sin_family = AF_INET;
dest.sin_port = htons(8888);
dest.sin_addr.s_addr = inet_addr("127.0.0.1");
SOCKET client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
connect(client, (sockaddr*)& dest, sizeof(dest));
std::string ok;
char buf[512];
do
{
std::cin >> ok;
if (ok.size() > 0)
{
send(client, ok.c_str(), ok.length(), 0);
ZeroMemory(&buf, sizeof(buf));
int okok = recv(client, buf, 512, 0);
if (okok > 0)
{
std::string str(buf);
std::cout <<">> "<< str << std::endl;
std::cout << std::endl;
// std::cout << std::string(buf, 0, okok) << std::endl;
}
}
} while (true);
system("pause");
return 0;
}
server.cpp
#include <iostream>
#include <WinSock2.h>
#include <WS2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
int main()
{
std::cout << "Server tcp/ip test V.14.0" << std::endl;
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
sockaddr_in server, client;
SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
// bind
bind(s, (sockaddr*)& server, sizeof(server));
//listen
listen(s, 3);
//Accept
sockaddr_in from; // for the client
int fromlen = sizeof(from);
SOCKET clientS = accept(s, (sockaddr*)& from, &fromlen);
if (clientS == INVALID_SOCKET)
{
std::cout << "error with accept()" << std::endl;
}
char recvbuf[512];
do
{
int iResult = recv(clientS, recvbuf, 512, 0);
if (iResult > 0)
{
send(clientS, recvbuf, iResult, 0);
}
} while(true);
system("pause");
return 0;
}

Server problems

I try to make multithreaded server (C++, Visual Studio 2017), but have a problem: every time I start launch my .exe I get connected to port 52428 (while I even not start client). Why is heppening and how should I get rid of it.
Server:
#include <winsock2.h>
#include <thread>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if(err != 0)
return -1;
sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons(8080);
local.sin_addr.s_addr = htonl(INADDR_ANY);
SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
bind(s, (struct sockaddr*)&local, sizeof(local));
cout << "Start server..." << endl;
int r = listen(s, 10000);
while(true)
{
sockaddr_in remote;
int j = sizeof(remote);
SOCKET newS = accept(s, (struct sockaddr*) &remote, &j);
if (newS == INVALID_SOCKET)
continue;
Socket sock = {newS, remote.sin_port, const_cast<const char*>(inet_ntoa(remote.sin_addr))}; // save socket, port and IP
cout << " Connect to port " << sock.port << endl;
thread thr(processing, ref(sock));
thr.join();
}
WSACleanup();
return 0;
}
Thank you for help.

How to create sockets in C++ with Winsock2?

I'm trying to create sockets with a video a found on youtube.
Here's the video: Link
I tried the code, just like the video, but it keeps giving me errors and I can't compile it.
sdkddkver.h: No such file or directory.
I'm using Dev, and also tried to put the sdkddkver.h code in the same directory, nothing happened. Downloaded the Windows SDK but still.
Here's my final code after the video:
#pragma comment(lib, "Ws2_32.lib");
#include <sdkddkver.h>
#include <conio.h>
#include <stdio.h>
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
using namespace std;
int main() {
long answer;
WSAData wsaData;
WORD DLLVERSION;
DLLVERSION = MAKEWORD(2,1);
answer = WSAStartup(DLLVERSION, &wsaData);
SOCKADDR_IN addr;
int addrlen = sizeof(addr);
SOCKET = sListen;
SOCKET = sConnect;
sConnect = socket(AF_INET, SOCK_STREAM, NULL);
addr.sin_addr.s_addr = inet.ad_addr("127.0.0.1");
addr.sin_family = AF_INET;
addr.sin_port = htons(1234);
sListen = socket(AF_INET, SOCK_STREAM, NULL);
bind (sListen, (SOCKADDR*)&addr, sizeof(addr));
listen(sListen, SOMAXCONN);
for (;;){
cout << "Awaiting incoming conection..." << endl;
if (sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen))
{
cout << "Connection Found"<< endl;
}
}
cin.get();
return 0;
}
Any help is appreciated! thank you!

Connecting to smtp server windows C++

I'm trying to create a client manager. I'have read some information about sockets. I'm doing my first steps and I've got my first problem.
This is my code:
#include <iostream>
#include <cstdio>
#include <winsock2.h>
#include <windows.h>
#pragma comment (lib, "ws2_32.lib")
using namespace std;
const int VERSION_MAJOR = 1;
const int VERSION_MINOR = 1;
int main()
{
WSADATA WSData;
SOCKET sock;
struct sockaddr_in addr;
WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData)
sock = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(25); // или любой другой порт...
hostent *server_adress = gethostbyname("smtp.gmail.com");
addr.sin_addr.s_addr = *((unsigned long *)server_adress->h_addr_list[0]);
int con = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
cout << "connect status " << con << '\n';
return 0;
}
connect() returns -1
why i can't connect? Where is mistake?
Please, help me
In the following line:
sock = socket(AF_INET, SOCK_STREAM, 0);
You do not specify a protocol. You should change it to
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
This is the kind of protocol you are going to want to establish with something such as a mailing service.