Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** server.c -- serwer używający gniazd strumieniowych
- */
- #include <cstdio>
- #include <cstdlib>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <pthread.h>
- #include <arpa/inet.h>
- #include <string.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <termios.h>
- #define MYPORT 10049 // port, z którym będą się łączyli użytkownicy
- #define SOCKADDR sockaddr
- #define SOCKET_ERROR -1
- #define SOCKET int
- #define closesocket close
- //HANDLE ghMutex;
- void gotoxy(int x,int y)
- {
- printf("%c[%d;%df",0x1B,y,x);
- }
- int mainSocket;
- void *receiver( void *ptr )
- {
- int bytesRecv = -1;
- char recvbuf;
- int posx = 0;
- int posy = 0;
- while (1)
- {
- bytesRecv = recv(mainSocket, &recvbuf, 1, 0);
- if (recvbuf == '\n' || recvbuf == '\r')
- {
- posy++;
- posx = 0;
- continue;
- }
- // bckspace
- else if (recvbuf == 8 && posx>0)
- {
- posx--;
- continue;
- }
- if (posy + 13 == 25)
- posy = 0;
- //WaitForSingleObject(
- // ghMutex, // handle to mutex
- // INFINITE); // no time-out interval
- gotoxy(posx, posy + 13);
- if (recvbuf != '\n')
- printf("%c", recvbuf);
- //ReleaseMutex(ghMutex);
- posx++;
- if (posx == 79)
- {
- posx = 0;
- posy++;
- }
- }
- }
- void print_separator()
- {
- gotoxy(0, 12);
- printf("--------------------------------------------------------------------------------");
- }
- int main(void)
- {
- //ghMutex = CreateMutex(
- // NULL, // default security attributes
- // FALSE, // initially not owned
- // NULL); // unnamed mutex
- mainSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (mainSocket == -1)
- {
- printf("Error creating socket");
- return 1;
- }
- sockaddr_in service;
- memset(&service, 0, sizeof(service));
- service.sin_family = AF_INET;
- service.sin_addr.s_addr = INADDR_ANY;
- service.sin_port = htons(MYPORT);
- printf("Server: 1, Klient: 2\n");
- int mode;
- scanf("%d", &mode);
- if (mode == 1)
- {
- if (bind(mainSocket, (SOCKADDR *)& service, sizeof(service)) == -1)
- {
- printf("bind() failed.\n");
- closesocket(mainSocket);
- return 1;
- }
- if (listen(mainSocket, 1) == -1)
- 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);
- }
- system("clear");
- print_separator();
- mainSocket = acceptSocket;
- pthread_t thread1;
- pthread_create( &thread1, NULL, receiver, NULL);
- }
- if (mode == 2)
- {
- if (connect(mainSocket, (SOCKADDR *)& service, sizeof(service)) == -1)
- {
- printf("Failed to connect.\n");
- return 1;
- }
- system("clear");
- print_separator();
- pthread_t thread1;
- pthread_create( &thread1, NULL, receiver, NULL);
- }
- int posx = 0, posy = 0;
- while (1)
- {
- char key = mygetch();
- return 0;
- send(mainSocket, &key, 1, 0);
- if (key == '\r')
- {
- posy++;
- posx = 0;
- continue;
- }
- // bckspace
- else if (key == 8 && posx>0)
- {
- posx--;
- key = ' ';
- send(mainSocket, &key, 1, 0);
- gotoxy(posx, posy);
- printf("%c", key);
- //ReleaseMutex(ghMutex);
- key = 8;
- send(mainSocket, &key, 1, 0);
- continue;
- }
- if (posy == 12)
- posy = 0;
- //WaitForSingleObject(
- // ghMutex, // handle to mutex
- // INFINITE); // no time-out interval
- gotoxy(posx, posy);
- if (key != '\r')
- printf("%c", key);
- //ReleaseMutex(ghMutex);
- posx++;
- if (posx == 79)
- {
- posx = 0;
- posy++;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement