Skip to content

Instantly share code, notes, and snippets.

@lehaiquantb
Last active March 26, 2020 19:14
Show Gist options
  • Save lehaiquantb/6026325df360c9a3486fa2cdc14e2a04 to your computer and use it in GitHub Desktop.
Save lehaiquantb/6026325df360c9a3486fa2cdc14e2a04 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include<iostream>
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32")
using namespace std;
char command[50];
char domain[20];
int port;
int main()
{
cin >> command;
cin >> domain;
cin >> port;
//Khoi tao thu vien
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
//Tao socket cho client
SOCKET client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Phan giai ten mien
addrinfo* info;
SOCKADDR_IN addr;
int ret = getaddrinfo(domain, "http", NULL, &info);
if (ret != 0)
{
printf("getaddrinfo() failed\n");
return 1;
}
memcpy(&addr, info->ai_addr, info->ai_addrlen);
system("pause");
//Ket noi client voi server
ret = connect(client, (SOCKADDR*)&addr, sizeof(addr));
if (ret == SOCKET_ERROR)
{
ret = WSAGetLastError();
printf("connect() failed: error code %d\n", ret);
return 1;
}
const char *buf = "Thong tin o cung \r\n C:\\ 100GB \r\n D:\\ 128GB";
//Gui len server
send(client, buf, strlen(buf), 0);
closesocket(client);
WSACleanup();
}
#include <stdio.h>
#include <iostream>
#include <winsock2.h>
using namespace std;
#pragma comment(lib, "ws2_32")
char command[50];
int port;
int main()
{
cin >> command >> port;
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
SOCKET listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
bind(listener, (SOCKADDR*)&addr, sizeof(addr));
listen(listener, 5);
SOCKET client = accept(listener, NULL, NULL);
char buf[256];
int ret = recv(client, buf, sizeof(buf), 0);
if (ret <= 0)
{
printf("recv() failed\n");
return 1;
}
buf[ret] = 0;
printf("Received: %s\n", buf);
closesocket(client);
closesocket(listener);
WSACleanup();
}
#include <stdio.h>
#include<iostream>
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#pragma comment(lib, "ws2_32")
using namespace std;
char command[100];
char ip[20];
int port;
int main()
{
int i;
int j = 0;
cin >> command;
cin >> ip;
cin >> port;
//Khoi tao thu vien
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
//Tao socket cho client
SOCKET client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//Gan dia chi va cong cho server
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip);
addr.sin_port = htons(port);
//Ket noi client voi server
int ret = connect(client, (SOCKADDR*)&addr, sizeof(addr));
if (ret == SOCKET_ERROR)
{
ret = WSAGetLastError();
printf("connect() failed: error code %d\n", ret);
return 1;
}
char buf[256];
cout << "Nhap du lieu tu ban phim" << endl;
cin >> buf;
//Gui len server
send(client, buf, strlen(buf), 0);
closesocket(client);
WSACleanup();
}
#include <stdio.h>
#include<iostream>
#include<string>
using namespace std;
#include <winsock2.h>
#pragma comment(lib, "ws2_32")
#pragma warning(disable:4996)
char command[50];
int port;
string fileSentName;
string fileRecvName;
int main()
{
cin >> command;
cin >> port;
cin >> fileSentName;
cin >> fileRecvName;
//Khoi tao thu vien
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
//tao socket listener cho server
SOCKET listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
//gan dia chi va cong cho socket
bind(listener, (SOCKADDR*)&addr, sizeof(addr));
listen(listener, 5);
//tao socket cho ket noi cua client voi server
SOCKET client = accept(listener, NULL, NULL);
fileSentName = "D:\\Study\\20192\\Network Programming\\" + fileSentName;
const char* path = fileSentName.c_str();
// Doc du lieu tu file va gui sang server
FILE*f = fopen(path, "rb");
char bufSent[2048];
int ret,sum = 0;
while (1)
{
ret = fread(bufSent, 1, sizeof(bufSent), f);
if (ret <= 0)
break;
send(client, bufSent, ret, 0);
sum += ret;
cout << "Da gui " << sum << " bytes sang client"<< endl;
}
//ket thuc gui du lieu
cout << "Finished sent!!!";
fclose(f);
char buf[2048];
fileRecvName = "D:\\Study\\20192\\Network Programming\\" + fileRecvName;
path = fileRecvName.c_str();
f = fopen(path, "wb");
//nhan du lieu tu client va ghi vao file
while (1)
{
ret = recv(client, buf, sizeof(buf), 0);
if (ret <= 0)
break;
fwrite(buf, 1, ret, f);
sum += ret;
cout << "Nhan duoc " << sum << " bytes"<<endl;
}
//ket thuc nhan du lieu
cout <<"Finished recv!!!";
fclose(f);
closesocket(client);
closesocket(listener);
WSACleanup();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment