Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 1. Initialize Winsock.
- 2. Create a socket.
- 3. Connect to the server.
- 4. Send and receive data.
- 5. Disconnect.
- */
- #ifndef WIN32_LEAN_AND_MEAN
- #define WIN32_LEAN_AND_MEAN
- #define C_USER "USER %srn"
- #define C_PASS "PASS %srn"
- #define C_STAT "STATrn"
- #endif
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <iphlpapi.h>
- #include <stdio.h>
- #include <iostream>
- #include <stdlib.h>
- #include <conio.h>
- #include <errno.h>
- #pragma comment(lib, "Ws2_32.lib")
- using namespace std;
- int main() {
- //instalacja winsocka
- WSADATA wsaData;
- int iResult;
- // Initialize Winsock
- iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
- if (iResult != 0) {
- printf("dupa1 %d\n", iResult);
- return 1;
- }
- struct addrinfo *result = NULL,
- *ptr = NULL,
- hints;
- ZeroMemory( &hints, sizeof(hints) );
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- #define DEFAULT_PORT "110"
- iResult = getaddrinfo("poczta.wp.pl",DEFAULT_PORT,&hints,&result);
- if (iResult != 0) {
- printf("dupa socket nie polaczyl sie z wesola poczta %d\n", iResult); // co robi 3 argument funkcji getaddrinfo??
- WSACleanup();
- return 1;
- }
- //tworzymy socketa odbierajacego i wysylajacego dane
- SOCKET ConnectSocket = INVALID_SOCKET;
- ptr=result;
- ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
- ptr->ai_protocol);
- if (ConnectSocket == INVALID_SOCKET) {
- printf("dupa3 socket odbierajacy cos z poczty nie dziala: %ld\n", WSAGetLastError());
- freeaddrinfo(result);
- WSACleanup();
- return 1;
- }
- //laczenie sie z serwerem
- iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- closesocket(ConnectSocket);
- ConnectSocket = INVALID_SOCKET;
- }
- freeaddrinfo(result);
- //spr czy sie polaczyl
- if (ConnectSocket == INVALID_SOCKET) {
- printf(" dupa Unable to connect to server!\n");
- WSACleanup();
- return 1;
- }
- // wyslanie loginu
- char sendbuf[0x64] = {0};
- char recvbuf[0x64]={0};
- sprintf(sendbuf, C_USER, "poczta_sieci@o2.pl");
- iResult = send(ConnectSocket, sendbuf, strlen(sendbuf), 0);
- iResult = recv(ConnectSocket, recvbuf,512, 0);
- printf("%s",recvbuf);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement