Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <WS2tcpip.h>
- #include <string>
- #include <conio.h>
- #include <limits>
- #include <thread>
- #include <windows.h>
- #pragma comment(lib, "ws2_32.lib")
- using namespace std;
- void getmessage(char buf[4096], SOCKET sock, char host[NI_MAXHOST])
- {
- //Wait
- int bytesReceived;
- while (true) {
- ZeroMemory(buf, 4096);
- bytesReceived = recv(sock, buf, 4096, 0);
- if (bytesReceived == 0)
- {
- std::cout << "\t\t\tRecieved 0 bytes" << std::endl;
- Sleep(1000);
- break;
- }
- else
- {
- std::cout << "\t\t\t" << host << ": " << buf << "\r\a";
- }
- }
- }
- int main()
- {
- string ipAddress;
- int port;
- cout << "Ip address to connect to: ";
- cin >> ipAddress;
- cout << "Port: ";
- cin >> port;
- //Initialize Winsock
- WSAData data;
- WORD ver = MAKEWORD(2, 2);
- int wsResult = WSAStartup(ver, &data);
- if (wsResult != 0)
- {
- cerr << "Failed to start" << endl;
- return 0;
- }
- //Create socket
- SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
- if (sock == INVALID_SOCKET)
- {
- cerr << "Failed to start" << endl;
- return 0;
- }
- //Fill in a hint structure
- sockaddr_in hint;
- hint.sin_family = AF_INET;
- hint.sin_port = htons(port);
- inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr);
- //Connect to server
- int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
- if (connResult == SOCKET_ERROR)
- {
- int LastError = WSAGetLastError();
- if (LastError == 10013) {
- cout << "Permission denied" << endl;
- }
- if (LastError == 10048) {
- cout << "Address already in use." << endl;
- }
- if (LastError == 10049) {
- cout << "Cannot assign requested address." << endl;
- }
- if (LastError == 10047) {
- cout << "Address family not supported by protocol family." << endl;
- }
- if (LastError == 10037) {
- cout << "Operation already in process." << endl;
- }
- if (LastError == 10053) {
- cout << "Software caused connection abort." << endl;
- }
- while (LastError == 10060)
- {
- if (connect(sock, (sockaddr*)&hint, sizeof(hint)))
- {
- goto messaging;
- }
- else {
- cout << "Connection timed out." << endl;
- }
- }
- if (LastError == 10054) {
- cout << "Connection reset by peer." << endl;
- }
- if (LastError == 10039) {
- cout << "Destion address required." << endl;
- }
- if (LastError == 10014) {
- cout << "Invalid address." << endl;
- }
- if (LastError == 10064) {
- cout << "Host is down." << endl;
- }
- if (LastError == 10065) {
- cout << "No route to host." << endl;
- }
- if (LastError == 10036) {
- cout << "Operation now in process." << endl;
- }
- if (LastError == 10050) {
- cout << "Network is down." << endl;
- }
- if (LastError == 10061) {
- cout << "Connection refused" << endl;
- }
- else {
- cerr << "Error: #" << WSAGetLastError() << endl;
- }
- cout << "Press 'ENTER' to close program";
- cin.ignore();
- cin.get();
- closesocket(sock);
- WSACleanup();
- return 0;
- }
- //Do-while loop to send and recieve data
- messaging:
- char buf[4096];
- string userInput;
- do
- {
- //Promt
- system("cls");
- char host[NI_MAXHOST]; //Remote name
- char service[NI_MAXSERV]; //Service or port
- ZeroMemory(host, NI_MAXHOST);
- ZeroMemory(service, NI_MAXSERV);
- if (getnameinfo((sockaddr*)&hint, sizeof(hint), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
- {
- system("cls");
- std::cout << host << " joined the chat" << std::endl;
- }
- else
- {
- inet_ntop(AF_INET, &hint.sin_addr, host, NI_MAXHOST);
- std::cout << host << " joined the chat" << std::endl;
- }
- //Wait
- thread getmessaget(getmessage, buf, sock, host);
- cout << "Your message: ";
- cin.ignore();
- getline(cin, userInput);
- //Send
- int sendResult = send(sock, userInput.c_str(), userInput.size() + 1, 0);
- if (sendResult == SOCKET_ERROR)
- {
- cout << "error";
- Sleep(2000);
- goto close;
- }
- } while (userInput.size() > 0);
- //Gracefully close down everything
- close:
- closesocket(sock);
- WSACleanup();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement