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.
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'm trying to write a minimal HTTP server on Windows and see the response in Chrome with http://127.0.0.1/5000. The following code works sometimes ("Hello World"), but the request fails half of the time with ERR_CONNECTION_ABORTED in Chrome (even after I restart the server). Why?
Error:
#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA WSAData;
SOCKET sock, csock;
SOCKADDR_IN sin, csin;
WSAStartup(MAKEWORD(2,0), &WSAData);
sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_family = AF_INET;
sin.sin_port = htons(5000);
bind(sock, (SOCKADDR *) &sin, sizeof(sin));
listen(sock, 0);
while(1)
{
int sinsize = sizeof(csin);
if((csock = accept(sock, (SOCKADDR *) &csin, &sinsize)) != INVALID_SOCKET)
{
std::string response = "HTTP/1.1 200 OK\nConnection: close\nContent-Length: 11\n\nHello World";
send(csock, response.c_str(), response.size(), 0);
std::cout << "done";
closesocket(csock);
}
}
return 0;
}
You fail to read the client's request before closing the connection. This usually results in the server sending a RST back to the client, which can cause the ERR_CONNECTION_ABORTED when it is processed before the response itself was processed.
As observed by another (deleted) answer, this can be "mitigated" by adding some short delay before the connection is closed, so that the response is processed by the client.
The right fix is to read the request from the client, though.
Apart from that, your response is not valid HTTP since you use \n instead of \r\n as line end and header end.
Working solution:
#include <winsock2.h>
#include <iostream>
#define DEFAULT_BUFLEN 8192
#pragma comment(lib, "ws2_32.lib")
int main()
{
char recvbuf[DEFAULT_BUFLEN];
WSADATA WSAData;
SOCKET sock, csock;
SOCKADDR_IN sin, csin;
WSAStartup(MAKEWORD(2,0), &WSAData);
sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_family = AF_INET;
sin.sin_port = htons(5000);
bind(sock, (SOCKADDR *) &sin, sizeof(sin));
listen(sock, 0);
while (1)
{
int sinsize = sizeof(csin);
if ((csock = accept(sock, (SOCKADDR *) &csin, &sinsize)) != INVALID_SOCKET)
{
recv(csock, recvbuf, DEFAULT_BUFLEN, 0);
std::string response = "HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Length: 11\r\n\r\nHello World";
send(csock, response.c_str(), response.size(), 0);
std::cout << "done";
closesocket(csock);
}
}
return 0;
}
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'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!
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).