Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////
- // Simple Chat Client, as basic as it gets
- // thx dxtuts
- ////////////////////////////////////////////////////
- #define _WINSOCKAPI_ // Don't include Winsock.h
- #include <winsock2.h> // WinSock header file
- #include <stdio.h> // Input/Output header file for gets() function
- #pragma comment(lib, "Ws2_32.lib") // WinSock Library
- #define SERVER_ADDRESS "192.168.1.100" // ip where server is, yours will differ
- #define SERVER_PORT 17000
- DWORD WINAPI RecvThread(LPVOID Whatever); // The RecvThread() prototype
- WSADATA Winsock; // Stores information about Winsock
- SOCKET Socket; // The ID of the socket
- sockaddr_in ServerAddress; // The address to send data to
- char SendBuffer[256]; // The buffer of data to send
- char RecvBuffer[256]; // The buffer of data to receive
- int SizeInt = sizeof(ServerAddress); // The size of the server's address
- void main()
- {
- WSAStartup(MAKEWORD(2, 2), &Winsock);
- if(LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2) // Check version
- {
- WSACleanup();
- return;
- }
- // Make the Socket
- Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- // Input Server Information
- ZeroMemory(&ServerAddress, sizeof(ServerAddress)); // clear the struct
- ServerAddress.sin_family = AF_INET; // set the address family
- ServerAddress.sin_addr.s_addr = inet_addr(SERVER_ADDRESS); // set the IP address
- ServerAddress.sin_port = SERVER_PORT; // set the port
- // Send a knock to the server
- SendBuffer[0] = 1;
- sendto(Socket, SendBuffer, 1, 0, (sockaddr*)&ServerAddress, sizeof(sockaddr));
- // Start the receiver thread
- CreateThread(NULL, 0, RecvThread, NULL, 0, NULL);
- // Send the Messages
- while(true)
- {
- gets(SendBuffer);
- sendto(Socket, SendBuffer, 256, 0, (sockaddr*)&ServerAddress, sizeof(sockaddr));
- if(SendBuffer[0] == ' ')
- break;
- };
- WSACleanup();
- return;
- }
- DWORD WINAPI RecvThread(LPVOID Whatever)
- {
- while(true)
- {
- // Wait for messages to arrive
- recvfrom(Socket, RecvBuffer, 256, 0, (sockaddr*)&ServerAddress, &SizeInt);
- // Print them to the screen
- printf("Received: "); // print a pretty "Received: " label
- printf(RecvBuffer); // print the data itself
- printf("\n"); // jump to a new line
- // If the user quits, this thread has to exit too, or the program won't end
- if(SendBuffer[0] == ' ')
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment