Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SERWER
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #include <cstdio>
- #include <cstdlib>
- #include <winsock2.h>
- #include <iostream>
- #include <string>
- #pragma comment(lib, "Ws2_32.lib")
- using namespace std;
- int main()
- {
- WSADATA wsaData;
- int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (result != NO_ERROR)
- printf("Initialization error.\n");
- SOCKET mainSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (mainSocket == INVALID_SOCKET)
- {
- printf("Error creating socket: %ld\n", WSAGetLastError());
- WSACleanup();
- return 1;
- }
- sockaddr_in service;
- memset(&service, 0, sizeof(service));
- service.sin_family = AF_INET;
- service.sin_addr.s_addr = inet_addr("127.0.0.1");
- service.sin_port = htons(27015);
- if (bind(mainSocket, (SOCKADDR *)& service, sizeof(service)) == SOCKET_ERROR)
- {
- printf("bind() failed.\n");
- closesocket(mainSocket);
- return 1;
- }
- if (listen(mainSocket, 1) == SOCKET_ERROR)
- printf("Error listening on socket.\n");
- SOCKET acceptSocket = SOCKET_ERROR;
- printf("Waiting for a client to connect...\n");
- while (acceptSocket == SOCKET_ERROR)
- {
- acceptSocket = accept(mainSocket, NULL, NULL);
- }
- printf("Client connected.\n");
- mainSocket = acceptSocket;
- int bytesSent;
- int bytesRecv = SOCKET_ERROR;
- const int size = 32;
- char sendbuf[1];
- char recvbuf[size];
- bytesRecv = recv(mainSocket, recvbuf, size, 0);
- printf("Bytes received: %ld\n", bytesRecv);
- printf("Received text: %s\n", recvbuf);
- string t = recvbuf;
- cout << t.size() << endl;
- //printf("Bytes sent: %ld\n", bytesSent);
- int c=0;
- for (int i = 0; i < min(size, t.size()); i++) {
- sendbuf[0] = t[i];
- bytesSent = send(mainSocket, sendbuf, 1, 0);
- c++;
- }
- //bytesSent = send(mainSocket, sendbuf, strlen(sendbuf), 0);
- printf("Bytes sent: %ld\n", bytesSent);
- cout << "C: " << c << endl;
- bytesSent = shutdown(mainSocket, SD_SEND);
- if (bytesSent == SOCKET_ERROR) {
- printf("shutdown failed with error: %d\n", WSAGetLastError());
- closesocket(mainSocket);
- WSACleanup();
- }
- system("pause");
- }
- KLIENT
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #include <cstdio>
- #include <cstdlib>
- #include <winsock2.h>
- #include <iostream>
- #include <string>
- #include <iostream>
- #pragma comment(lib, "Ws2_32.lib")
- using namespace std;
- int main()
- {
- cout << "podaj tekst do przeslania" << endl;
- string t;
- getline(cin, t);
- WSADATA wsaData;
- int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (result != NO_ERROR)
- printf("Initialization error.\n");
- SOCKET mainSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (mainSocket == INVALID_SOCKET)
- {
- printf("Error creating socket: %ld\n", WSAGetLastError());
- WSACleanup();
- return 1;
- }
- sockaddr_in service;
- memset(&service, 0, sizeof(service));
- service.sin_family = AF_INET;
- service.sin_addr.s_addr = inet_addr("127.0.0.1");
- service.sin_port = htons(27015);
- if (connect(mainSocket, (SOCKADDR *)& service, sizeof(service)) == SOCKET_ERROR)
- {
- printf("Failed to connect.\n");
- WSACleanup();
- return 1;
- }
- int bytesSent;
- int bytesRecv = SOCKET_ERROR;
- const int size = 32;
- char sendbuf[size];
- for (int i = 0; i < min(size,t.size()); i++) {
- sendbuf[i] = t[i];
- }
- char recvbuf[size] = "";
- bytesSent = send(mainSocket, sendbuf, strlen(sendbuf), 0);
- printf("Bytes sent: %ld\n", bytesSent);
- while (1)
- {
- bytesRecv = recv(mainSocket, recvbuf, 1, 0);
- if (bytesRecv == 0 || bytesRecv == WSAECONNRESET)
- {
- printf("Connection closed.\n");
- break;
- }
- if (bytesRecv < 0)
- return 1;
- printf("Bytes received: %ld\n", bytesRecv);
- printf("Received text: %s\n", recvbuf);
- }
- while (1) {
- if (connect(mainSocket, (SOCKADDR *)& service, sizeof(service)) == SOCKET_ERROR) {
- cout << "Zakonczono prace!"<<endl;
- system("pause");
- return 0;
- }
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement