- #define WIN32_LEAN_AND_MEAN
- #include <process.h>
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include "memwatch.h"
- // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
- #pragma comment (lib, "Ws2_32.lib")
- #pragma comment (lib, "Mswsock.lib")
- #pragma comment (lib, "AdvApi32.lib")
- #define DEFAULT_BUFLEN 512
- #define DEFAULT_PORT "81"
- WSADATA wsaData;
- SOCKET ConnectSocket = INVALID_SOCKET; // PISANIE
- SOCKET ListenSocket = INVALID_SOCKET; // SŁUCHANIE
- SOCKET ClientSocket = INVALID_SOCKET; // SŁUCHANIE
- struct addrinfo *result = NULL;
- struct addrinfo hints;
- struct element
- {
- char * dane;
- struct element * next;
- };
- typedef struct element * segment;
- // typedef struct element * segment;
- char *sendbuf = "gnieciona bulka\0";
- char s[512], tmp[512];
- char recvbuf[DEFAULT_BUFLEN] = "\0";
- int gadanie = 1;
- int iResult;
- int recvbuflen = DEFAULT_BUFLEN;
- int i=0;
- int blad = 0;
- int wybor = 0;
- int pierwszy = 1;
- int port1, port2;
- segment glowa = NULL;
- HANDLE hRunMutex; /* "Keep Running" mutex */
- int ThreadNr; /* Number of threads started */
- void KbdFunc( void ); /* Keyboard input, thread dispatch */
- void ShutDown( void ); /* Program shutdown */
- int pisanie ();
- int sluchanie ( void *pMyID );
- void watek_sluchania( void );
- void read (segment ostatni);
- segment create (segment ostatni, char * slowo);
- segment delete_all (segment ostatni);
- void update (segment ostatni,int pozycja, char * slowo);
- int __cdecl main(int argc, char **argv)
- {
- // inicjuje Winsock'a
- iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
- if (iResult != 0)
- {
- printf("WSAStartup failed with error: %d\n", iResult);
- return 1;
- }
- ZeroMemory( &hints, sizeof(hints) );
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- printf("Twoj wybor:");
- scanf("%d", &wybor);
- if (wybor==1)
- {
- port1 = "83";
- port2 = "81";
- }
- else if (wybor == 2)
- {
- port1 = "81";
- port2 = "83";
- }
- watek_sluchania();
- Sleep(550);
- printf("Gotow?");
- getchar();
- while(gadanie)
- {
- if(pierwszy == 1)
- {
- // system("CLS");
- //printf("PISANIE");
- }
- pierwszy = 0;
- powtorka:
- iResult = getaddrinfo( "localhost", port1, &hints, &result ); // bez parametrow
- if ( iResult != 0 )
- {
- // printf("getaddrinfo failed with error: %d\n", iResult);
- // system("PAUSE");
- // WSACleanup();
- // return 1;
- goto powtorka;
- }
- // Attempt to connect to an address until one succeeds
- // Create a SOCKET for connecting to server
- ConnectSocket = socket(result->ai_family, result->ai_socktype,
- result->ai_protocol);
- if (ConnectSocket == INVALID_SOCKET) {
- // printf("socket failed with error: %ld\n", WSAGetLastError());
- // system("PAUSE");
- goto powtorka;
- // WSACleanup();
- // return 1;
- }
- // Connect to server.
- do {
- iResult = connect( ConnectSocket, result->ai_addr, (int)result->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- //WSACleanup();
- // printf("socket error");
- goto powtorka;
- //system("PAUSE");
- //return 1;
- }
- }
- while(iResult < 0);
- freeaddrinfo(result);
- if (ConnectSocket == INVALID_SOCKET) {
- // printf("Unable to connect to server!\n");
- // system("PAUSE");
- goto powtorka;
- // WSACleanup();
- // return 1;
- }
- do { // wysylanie informacji
- if (blad == 0)
- {
- printf("\nJa: ");
- fgets( s, 512, stdin );
- s[strlen(s)-1] = '\0';
- if (strncmp(s,"koniec",4)==0) gadanie = 0;
- glowa = create(glowa,s);
- //scanf("%s", s);
- sendbuf = s; //wskaznik do zmiennej s do ktorej dopisuje informacje
- }
- iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
- if (iResult == SOCKET_ERROR) {
- // printf("send failed with error: %d\n", WSAGetLastError());
- closesocket(ConnectSocket);
- iResult = shutdown(ConnectSocket, SD_SEND);
- // system("PAUSE");
- blad = 1;
- goto powtorka;
- // WSACleanup();
- // return 1;
- }
- blad = 0;
- printf("\n");
- iResult = 0;
- } while( iResult > 0 );
- iResult = shutdown(ConnectSocket, SD_SEND);
- closesocket(ConnectSocket);
- pierwszy = 0;
- continue;
- }
- printf("stop");
- read(glowa);
- glowa = create(glowa,"mis");
- read(glowa);
- system("PAUSE");
- ShutDown();
- return 0;
- }
- int sluchanie ( void *pMyID )
- {
- while(1)
- {
- if (pierwszy == 1)
- {
- system("CLS");
- //printf("SLUCHANIE\n\n");
- printf("Konwersacja z: %s\n\n", "localhost" );
- }
- pierwszy = 0;
- /* SŁUCHANIE ----------------------------------------------------------------*/
- // Resolve the server address and port
- iResult = getaddrinfo("localhost", port2, &hints, &result);
- if ( iResult != 0 ) {
- printf("getaddrinfo failed with error: %d\n", iResult);
- system("PAUSE");
- // WSACleanup();
- // return 1;
- }
- // Create a SOCKET for connecting to server
- ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
- if (ListenSocket == INVALID_SOCKET) {
- printf("socket failed with error: %ld\n", WSAGetLastError());
- system("PAUSE");
- freeaddrinfo(result);
- // WSACleanup();
- // return 1;
- }
- // Setup the TCP listening socket
- iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- printf("bind failed with error: %d\n", WSAGetLastError());
- system("PAUSE");
- freeaddrinfo(result);
- closesocket(ListenSocket);
- // WSACleanup();
- // return 1;
- }
- //freeaddrinfo(result);
- iResult = listen(ListenSocket, 1);
- if (iResult == SOCKET_ERROR) {
- printf("listen failed with error: %d\n", WSAGetLastError());
- system("PAUSE");
- closesocket(ListenSocket);
- // WSACleanup();
- // return 1;
- }
- // Accept a client socket
- ClientSocket = accept(ListenSocket, NULL, NULL);
- // printf("wysluchane");
- if (ClientSocket == INVALID_SOCKET) {
- printf("accept failed with error: %d\n", WSAGetLastError());
- system("PAUSE");
- closesocket(ListenSocket);
- // WSACleanup();
- // return 1;
- }
- // odbieranie póki klient nie zrezygnuje z wysylania
- do {
- for(i=0; i<recvbuflen; i++)
- {
- recvbuf[i] ='\0';
- }
- iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
- if (iResult > 0) {
- printf("\n%s: ", "localhost");
- printf(" %s\n", recvbuf );
- update (glowa,1,recvbuf);
- read(glowa);
- }
- else if (iResult == 0)
- iResult=iResult;
- else {
- printf("recv failed with error: %d\n", WSAGetLastError());
- system("PAUSE");
- }
- } while (iResult > 0);
- closesocket(ListenSocket);
- closesocket(ClientSocket);
- continue;
- }
- }
- void ShutDown( void ) /* Shut down threads */
- {
- while ( ThreadNr > 0 )
- {
- /* Tell thread to die and record its death. */
- ReleaseMutex( hRunMutex );
- ThreadNr--;
- }
- /* Clean up display when done */
- // WaitForSingleObject( hScreenMutex, INFINITE );
- // ClearScreen();
- }
- void watek_sluchania( void ) /* Dispatch and count threads. */
- {
- ThreadNr++;
- _beginthread( sluchanie, 0, &ThreadNr );
- ShutDown();
- }
- segment create (segment ostatni, char * slowo)
- {
- segment nowy = malloc( sizeof( segment) ); //zarezerwowanie pamięci
- nowy->dane = slowo; // zapisanie danych
- nowy->next = ostatni; //podpięcie pod stara glowe
- read(nowy); //zwrocenie jako nowa glowe
- return nowy;
- }
- void read (segment ostatni)
- {
- int i = 0;
- segment poprzedni = ostatni;
- while (poprzedni != NULL)
- {
- i++;
- printf("\n%d . %s",i,poprzedni->dane);
- poprzedni = poprzedni->next;
- }
- }
- segment delete_all (segment ostatni)
- {
- segment poprzedni;
- while (ostatni != NULL)
- {
- poprzedni = ostatni->next;
- free (ostatni);
- ostatni = poprzedni;
- }
- return (ostatni);
- }
- void update (segment ostatni,int pozycja, char * slowo)
- {
- int i;
- segment poprzedni = ostatni;
- for (i=1;i<pozycja;i++)
- {
- poprzedni= poprzedni->next;
- }
- poprzedni->dane = slowo;
- }