All pastes #286863 Raw Edit

clientas

public cpp v1 · immutable
#286863 ·published 2006-12-20 23:26 UTC
rendered paste body
#include <stdio.h>#include "winsock.h"#include <iostream>using namespace std;/* =======================================================================================================================    CONNECT ======================================================================================================================= */int connect (SOCKET s, char *hostname, unsigned short portnum){    /*~~~~~~~~~~~~*/    sockaddr_in sa;    hostent     *hp;    /*~~~~~~~~~~~~*/    hp = gethostbyname(hostname);    if (hp == NULL)    {        return -1;    }    memset(&sa, 0, sizeof(sa));    memcpy((char *) &sa.sin_addr, hp->h_addr, hp->h_length);    sa.sin_family = hp->h_addrtype;    sa.sin_port = htons((u_short) portnum);    if (connect(s, (struct sockaddr *) &sa, sizeof sa) == SOCKET_ERROR)    {        closesocket(s);        return -2;    }    return 0;}/* =======================================================================================================================    MAIN ======================================================================================================================= */int main (){    WSADATA info;    if (WSAStartup(MAKEWORD(1, 1), &info) != 0)    {        cout << "!WSAStartup() nepavyko" << endl;        return -1;    }    SOCKET  sock = socket(AF_INET, SOCK_STREAM, 0);    if (sock == INVALID_SOCKET)    {        printf("!neiseina sukurti soketo\n");        return -1;    }    unsigned short port;    cout << "portas:" << endl;    cin >> port;    if (connect(sock, "localhost", port) != 0)    {        cout << "!neisejo prisijungti prie hosto\n";        return -1;    }    char    inbuff[1] = "";    char    inbuff2[1] = "";    char    outbuff[100] = "";    char    ats[100] = "";    for (;;)    {        // RECIEVE        while (inbuff[0] != '#')            if (recv(sock, inbuff, 1,0) > 0)                if (inbuff[0] != '#') cout << inbuff[0];        cout << "\nkomanda: ";        cin >> outbuff;                outbuff[strlen(outbuff)] = '#';                // SEND        send(sock, outbuff, (int) strlen(outbuff), 0);         //---------------(  EXIT )----------------------------        if (!strcmp(outbuff, "2#"))        {            memset(inbuff, 0, 1);            if (recv(sock, inbuff, 1, 0) > 0) break;        }//-----------(  [1]Operator chat  )-------------------	if (!strcmp(outbuff, "1#"))	{	  // Recieve_inner	  cout << "Op sako[1]: " << endl;	  while (inbuff2[0] != '#')	    if (recv(sock, inbuff2, 1,0) > 0)	      if (inbuff2[0] != '#') cout << inbuff2[0];	  for (;;)	  { 	    memset(inbuff, 0, 1);	    memset(outbuff, 0, 50);	    while (inbuff[0] != '#')	    {	      if (recv(sock, inbuff, 1, 0) > 0) strncat (ats, inbuff, 1);	    }	    cout << "\n Op sako2: \n" << ats << endl;            cout << "\nKalbekite toliau" << endl;	    cin >> outbuff;	    outbuff[strlen(outbuff)] = '#';	    send(sock, outbuff, (int) strlen(outbuff), 0); 	  }   	}            // NULL BUFFERS        memset(inbuff, 0, 1);        memset(outbuff, 0, 10);        cout << "-" << endl;    }    return 0;}