socket 简单聊天示例

客户端

#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <stdio.h> 
#include <time.h> 

#pragma comment(lib, "wsock32.lib")

#pragma comment(lib, "Ws2_32.lib")

using namespace std;

SOCKET ConnectSocket;

DWORD WINAPI ThreadProc(

    LPVOID lpParameter  
    ){

        char test[99];

        ZeroMemory(test,sizeof(test));

        while(recv(ConnectSocket,test,sizeof(test),0)){

            time_t rawtime; 

            struct tm * timeinfo; 

            time ( &rawtime ); 

            timeinfo = localtime ( &rawtime ); 

            cout<<asctime (timeinfo);

            cout<<"He(she) say :"<<test<<endl<<endl;

            ZeroMemory(test,sizeof(test));
        }

        return 0;
}

int main() {

    WSADATA wsaData;          //初始化的结构

    SOCKADDR_IN servAddr;       //套接口结构

    //SOCKET ConnectSocket = INVALID_SOCKET;     //失败就不变

    int iResult;                // >0就成功 这是不一定的.

    // Initialize Winsock

    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);   //初始化套接口 括号里面的是版本

    if (iResult != 0) {

        cout<<"WSAStartup failed";

        return -1;     //这是告诉系统失败的意思
    }

    ConnectSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);   //(协议,套接口 字符串,TCP协议)


    if (ConnectSocket == INVALID_SOCKET) {

        cout<<"Error at socket():\n";

        WSACleanup();

        return -1;
    }


    servAddr.sin_family=AF_INET;   //协议

    servAddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");

    servAddr.sin_port=htons(4999);   //端口

    iResult = connect(ConnectSocket,(LPSOCKADDR)&servAddr,sizeof(servAddr)); //(连接,传入,大小)

    if (iResult == SOCKET_ERROR) {

        cout<<"shost failed";

        closesocket(ConnectSocket);

        WSACleanup();

        return -1;

    }


    char test[99];

    ZeroMemory(test,99);

    CloseHandle(CreateThread(NULL,0,&ThreadProc,NULL,NULL,NULL));

    char shuru[99];


    while (gets(shuru)){

        strcpy(test,shuru);

        iResult = send(ConnectSocket,test,(int)strlen(test),0);

        cout<<endl;

        if (iResult == SOCKET_ERROR) {

            cout<<"出错了\n";

            closesocket(ConnectSocket);

            WSACleanup();

            return -1;
        }
    }



    closesocket(ConnectSocket);

    WSACleanup();

    return 0;
}

服务端

#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <stdio.h> 
#include <time.h> 

#pragma comment(lib, "Ws2_32.lib")

using namespace std;

SOCKET ClientSocket;

DWORD WINAPI ThreadProc(

    LPVOID lpParameter  
    ){


        char buff[99];

        ZeroMemory(buff,sizeof(buff));

        while(recv(ClientSocket,buff,sizeof(buff),0)){

            time_t rawtime; 

            struct tm * timeinfo; 

            time ( &rawtime ); 

            timeinfo = localtime ( &rawtime ); 

            cout<<asctime (timeinfo);

            cout<<"He(she) say :"<<buff<<endl<<endl;

            ZeroMemory(buff,sizeof(buff));

        }
        return 0;

}

int main() {


    WSADATA wsaData;

    SOCKADDR_IN servAddr;

    SOCKET ConnectSocket = INVALID_SOCKET;

    int iResult;

    // Initialize Winsock

    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);

    if (iResult != 0) {

        cout<<"WSAStartup failed: ";

        return 1;
    }

    ConnectSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    if (ConnectSocket == INVALID_SOCKET) {

        cout<<"Error at socket():\n";

        WSACleanup();

        return 1;
    }
    servAddr.sin_family=AF_INET;

    servAddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");

    servAddr.sin_port=htons(4999);

    iResult = bind( ConnectSocket,(LPSOCKADDR)&servAddr,sizeof(servAddr));

    if (iResult == SOCKET_ERROR) {

        cout<<"bind failed";

        closesocket(ConnectSocket);

        WSACleanup();

        return 1;
    }


    if ( listen( ConnectSocket, SOMAXCONN ) == SOCKET_ERROR ) {

        cout<< "Listen failed with error: %ld\n";

        closesocket(ConnectSocket);

        WSACleanup();

        return 1;
    }


    sockaddr_in addrClient;

    int addrClientlen=sizeof(addrClient);

    ClientSocket=accept(ConnectSocket,(sockaddr FAR*)&addrClient,&addrClientlen);


    if (ClientSocket == INVALID_SOCKET) {

        cout<<"accept failed: %d\n";

        closesocket(ConnectSocket);

        WSACleanup();

        return 1;

    }

    CloseHandle(CreateThread(NULL,0,&ThreadProc,NULL,NULL,NULL));


    char shuru[99];

    ZeroMemory(shuru,sizeof(shuru));

    while (gets(shuru)){

        iResult = send(ClientSocket,shuru , sizeof(shuru), 0);

        cout<<endl;

        if (SOCKET_ERROR==iResult)
        {
            cout<<"recv failed";

            closesocket(ConnectSocket);

            closesocket(ClientSocket);

            WSACleanup();

            return -1;
        }}


    closesocket(ConnectSocket);

    closesocket(ClientSocket);

    WSACleanup();

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值