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!
Related
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 want to make a program such that the user runs the client program and then writes the name of a file to be transferred to the server, the user should be able to set the server address. Then the server accepts the file and saves it to its working directory with the same name.
I don't know how to do this.
This is my code:
Server.cpp
#include <iostream>
#include <winsock2.h>
#pragma comment (lib, "ws2_32.lib")
using namespace std;
int main(int argc, _TCHAR* argv[])
{
WSAData wsaData;
WORD DLLVersion = MAKEWORD(2,1);
if (WSAStartup(DLLVersion, &wsaData)) {
cout << "Error\n";
exit(1);
}
SOCKADDR_IN addr;
int sizeofaddr = sizeof(addr);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(1111);
addr.sin_family = AF_INET;
SOCKET sListen = socket(AF_INET, SOCK_STREAM, NULL);
bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
listen(sListen, SOMAXCONN);
SOCKET newConnection;
newConnection = accept(sListen, (SOCKADDR*)&addr, &sizeofaddr);
if (newConnection == 0) cout << "Error #2\n";
else cout << "Client connected!\n";
system("pause");
return 0;
}
Client.cpp
#include <iostream>
#include <winsock2.h>
#pragma comment (lib, "ws2_32.lib")
using namespace std;
int main(int argc, _TCHAR* argv[])
{
WSAData wsaData;
WORD DLLVersion = MAKEWORD(2,1);
if (WSAStartup(DLLVersion, &wsaData)) {
cout << "Error\n";
exit(1);
}
SOCKADDR_IN addr;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(1111);
addr.sin_family = AF_INET;
SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL);
if (connect(Connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) {
cout << "Error: failed connect to server\n";
return 1;
}
cout << "Connected!\n";
system("pause");
return 0;
}
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;
}
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.
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).