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 <conio.h>
- #include <winsock2.h>
- #define MYPORT 10050 // port, z którym będą się łączyli użytkownicy
- HANDLE ghMutex;
- void gotoxy(int x, int y)
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
- }
- DWORD WINAPI receiver( LPVOID lpParam )
- {
- int mainSocket = (int)lpParam;
- int bytesRecv = SOCKET_ERROR;
- 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
- 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(10050);
- printf("Server: 1, Klient: 2\n");
- int mode;
- scanf_s("%d", &mode);
- if (mode == 1)
- {
- 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);
- }
- system("cls");
- print_separator();
- mainSocket = acceptSocket;
- DWORD threadId;
- CreateThread(
- NULL, // default security attributes
- 0, // use default stack size
- receiver, // thread function name
- (LPVOID)mainSocket, // argument to thread function
- 0, // use default creation flags
- &threadId); // returns the thread identifier
- }
- if (mode == 2)
- {
- if (connect(mainSocket, (SOCKADDR *)& service, sizeof(service)) == SOCKET_ERROR)
- {
- printf("Failed to connect.\n");
- WSACleanup();
- return 1;
- }
- system("cls");
- print_separator();
- DWORD threadId;
- CreateThread(
- NULL, // default security attributes
- 0, // use default stack size
- receiver, // thread function name
- (LPVOID)mainSocket, // argument to thread function
- 0, // use default creation flags
- &threadId); // returns the thread identifier
- }
- int posx = 0, posy = 0;
- while (1)
- {
- char key = _getch();
- 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