Socketプログラミング-1 V 1チャット


1 V 1チャットの小さい例を提供してみんなの参考に供します.直接コードを贴って、くだらない话は多く言いません.注:VS 2015コードのテストに成功して、その他のコンパイラは问题があれば自分で解决します.
まず効果図を1枚ください.
サービス:
#include <WinSock2.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib,"ws2_32.lib")

int main()
{
    //          
    WSADATA wsaData = { 0 };
    SOCKET ServerSocket = INVALID_SOCKET;//      
    SOCKET ClientSocket = INVALID_SOCKET;//      
    SOCKADDR_IN ServerAddr = { 0 };//     
    SOCKADDR_IN ClientAddr = { 0 };//     
    int iClientAddrLen = sizeof(ClientAddr);
    USHORT uPort = 18000;//       

                         //      
    if (WSAStartup(MAKEWORD(2, 2), &wsaData))
    {
        printf("WSAStartup failed with error code: %d
"
, WSAGetLastError()); return -1; } // if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { printf("wVersion was not 2.2
"
); return -1; } // ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ServerSocket == INVALID_SOCKET) { printf("socket failed with error code: %d
"
, WSAGetLastError()); return -1; } // ServerAddr.sin_family = AF_INET;// ServerAddr.sin_port = htons(uPort);// ServerAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);// // if (SOCKET_ERROR == bind(ServerSocket, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr))) { printf("bind failed with error code: %d
"
, WSAGetLastError()); closesocket(ServerSocket); return -1; } // if (SOCKET_ERROR == listen(ServerSocket, 1)) { printf("listen failed with error code: %d
"
, WSAGetLastError()); closesocket(ServerSocket); WSACleanup(); return -1; } // printf("Please input your name:"); char name[32] = { 0 }; gets_s(name); printf("waiting connect.....
"
); // ClientSocket = accept(ServerSocket, (SOCKADDR*)&ClientAddr, &iClientAddrLen); if (ClientSocket == INVALID_SOCKET) { printf("accept failed with error code: %d
"
, WSAGetLastError()); closesocket(ServerSocket); WSACleanup(); return -1; } printf("Successfuuly got a connection from IP:%s Port:%d



"
, inet_ntoa(ClientAddr.sin_addr),htons(ClientAddr.sin_port)); char buffer[4096] = {0}; int iRecvLen = 0; int iSendLen = 0; // iSendLen = send(ClientSocket, name, strlen(name), 0); if (SOCKET_ERROR == iSendLen) { printf("send failed with error code: %d
"
, WSAGetLastError()); closesocket(ServerSocket); closesocket(ClientSocket); WSACleanup(); return -1; } // char nameOther[32] = { 0 }; iRecvLen = recv(ClientSocket, nameOther, sizeof(nameOther), 0); if (SOCKET_ERROR == iRecvLen) { printf("recv failed with error code: %d
"
, WSAGetLastError()); closesocket(ServerSocket); closesocket(ClientSocket); WSACleanup(); return -1; } strcat(nameOther, "\0"); // while (1) { memset(buffer, 0, sizeof(buffer)); // iRecvLen = recv(ClientSocket, buffer, sizeof(buffer), 0); if (SOCKET_ERROR == iRecvLen) { printf("recv failed with error code: %d
"
, WSAGetLastError()); closesocket(ServerSocket); closesocket(ClientSocket); WSACleanup(); return -1; } //printf("recv %d bytes from %s: ", iRecvLen, nameOther); strcat(buffer, "\0"); // , printf("%s: %s
"
, nameOther, buffer); // memset(buffer, 0, sizeof(buffer)); printf("%s: ",name); gets_s(buffer); if (strcmp(buffer, "bye") == 0) break; iSendLen = send(ClientSocket, buffer, strlen(buffer), 0); if (SOCKET_ERROR == iSendLen) { printf("send failed with error code: %d
"
, WSAGetLastError()); closesocket(ServerSocket); closesocket(ClientSocket); WSACleanup(); return -1; } //printf("send %d bytes to %s
", iSendLen, nameOther);//
} closesocket(ClientSocket); closesocket(ServerSocket); WSACleanup(); system("pause"); return 0; }

クライアント:
#include <WinSock2.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib,"ws2_32.lib")

int main()
{
    WSADATA wsaData = { 0 };//       
    SOCKET ClientSocket = INVALID_SOCKET;//      
    SOCKADDR_IN ServerAddr = { 0 };//     
    USHORT uPort = 18000;//     

    //      
    if (WSAStartup(MAKEWORD(2, 2), &wsaData))
    {
        printf("WSAStartup failed with error code: %d
"
, WSAGetLastError()); return -1; } // if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { printf("wVersion was not 2.2
"
); return -1; } // ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ClientSocket == INVALID_SOCKET) { printf("socket failed with error code: %d
"
, WSAGetLastError()); return -1; } // IP printf("Please input server IP:"); char IP[32] = { 0 }; gets_s(IP); // printf("Please input your name:"); char name[32] = { 0 }; gets_s(name); // ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons(uPort);// ServerAddr.sin_addr.S_un.S_addr = inet_addr(IP);// printf("connecting......
"
); // if (SOCKET_ERROR == connect(ClientSocket, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr))) { printf("connect failed with error code: %d
"
, WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); return -1; } printf("connecting server successfully IP:%s Port:%d



"
, inet_ntoa(ServerAddr.sin_addr),htons(ServerAddr.sin_port)); char buffer[4096] = {0}; int iRecvLen = 0; int iSnedLen = 0; // iSnedLen = send(ClientSocket, name, strlen(name), 0); if (SOCKET_ERROR == iSnedLen) { printf("send failed with error code: %d
"
, WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); return -1; } // char nameOther[32] = { 0 }; iRecvLen = recv(ClientSocket, nameOther, sizeof(nameOther), 0); if (SOCKET_ERROR == iRecvLen) { printf("send failed with error code: %d
"
, WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); return -1; } strcat(nameOther, "\0"); // while (1) { memset(buffer, 0, sizeof(buffer)); // printf("%s: ", name); gets_s(buffer); if(strcmp(buffer,"bye") == 0) break; iSnedLen = send(ClientSocket, buffer, strlen(buffer), 0); if (SOCKET_ERROR == iSnedLen) { printf("send failed with error code: %d
"
, WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); return -1; } //printf("send %d bytes to %s
", iSnedLen, nameOther);//
memset(buffer, 0, sizeof(buffer)); // iRecvLen = recv(ClientSocket, buffer, sizeof(buffer), 0); if (SOCKET_ERROR == iRecvLen) { printf("send failed with error code: %d
"
, WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); return -1; } //printf("recv %d bytes from %s: ", iRecvLen, nameOther);// strcat(buffer, "\0"); printf("%s: %s
"
, nameOther,buffer); } closesocket(ClientSocket); WSACleanup(); system("pause"); return 0; }