如何制作全双工c ++ winsock聊天程序

时间:2012-12-04 08:37:00

标签: c++ winsock2

我正在尝试用C ++编写一个简单的客户端 - 服务器聊天程序。我写了这个,但我的问题是我可以将数据从服务器发送到客户端。我希望进行全双工通信,并将数据从客户端发送到服务器。

这是我的代码。

服务器:

#include "stdafx.h"
#include <WinSock2.h>
#include <winsock.h>
#include <stdio.h>
#include <iostream>
#include <string>
#define RCVBUFSIZE 32
using namespace std;

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



int main()
{
    WSADATA wsadat;
    SOCKET welcome_socket = INVALID_SOCKET;
    string IpAddress;
    int PortNom;
    SOCKADDR_IN SockAddress;

    //-----------------------------------------------
if (WSAStartup(MAKEWORD(2,2),&wsadat) !=NO_ERROR)
{
    cout << "WSA intialization failed. \n";
    WSACleanup();
    return 1;
}
//------------------------------------------------
welcome_socket = socket(AF_INET , SOCK_STREAM , 0);

//--------------------------------------------------
cout << "Enter your ip address: ";
cin >> IpAddress;

cout << "Enter your port nomber:";
cin >> PortNom;

//---------------------------------------------------

SockAddress.sin_addr.s_addr = inet_addr (IpAddress.c_str());
SockAddress.sin_port = htons(PortNom);
SockAddress.sin_family = AF_INET;

//---------------------------------------------------

bind(welcome_socket,(SOCKADDR *)&SockAddress,sizeof(SockAddress));
listen(welcome_socket,1);

//----------------------------------------------------

SOCKET Comm_sock;
SOCKADDR_IN ClientAddr;
int AddressLen = sizeof (ClientAddr);
char *buffer = new char[13];

int turn= 0;
char* buf = new char[50];
char temp;
while (true)
{
    Comm_sock = accept(welcome_socket,(SOCKADDR *)&ClientAddr , &AddressLen);
    while (buf != "exit")
    {
        if (turn == 0)
        {
            cout << endl;
            recv(Comm_sock,buf,50,0);

            if(buf[0]=='e' && buf[1]=='x' && buf[2]=='i' && buf[3]=='t')
                return 0;

            cout << "MsG from client :"<< buf << endl;
            cin.get(temp);
            turn = 1;
        }
        if  (turn == 1)
        {
            cout << "Enter MsG:";
            cin.get(buf,50);
            if (buf[0]=='e' && buf[1]=='x' && buf[2]=='i' && buf[3]=='t')
                return 0;
                send(Comm_sock,buf,strlen(buf)+1,0);
                cin.get(temp);
            turn=0; 
        }
    }
}

和客户:

#include "stdafx.h"
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <iostream>


using namespace std;
// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "12345"

//-------------------------------------
int main ()
{
    WSADATA wsadata;
    int iResult;

    SOCKET ConnectSocket = INVALID_SOCKET;
    struct sockaddr_in clientservice;

    char *sendbuf ="";
    char recvbuf[DEFAULT_BUFLEN];
    char sendbuffer[512];
    int recvbuflen = DEFAULT_BUFLEN;
    int sendbuflen = 512;

//----------------------------------

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

//----------------------------------

ConnectSocket = socket(AF_INET,SOCK_STREAM , IPPROTO_TCP);

//-----------------------------------

clientservice.sin_family = AF_INET;
clientservice.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    clientservice.sin_port = htons( 11111 );

 //-----------------------------------

iResult = connect (ConnectSocket,(SOCKADDR *)&clientservice      ,sizeof(clientservice));
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
cout << "bytes Sent:" << iResult << "\n";

//-------------------------------------

iResult = shutdown(ConnectSocket ,SD_SEND);
    if (iResult == SOCKET_ERROR) 
{
    printf("shutdown failed: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
}


    do {
        string reciev;
        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
         recvbuf[iResult]= 0 ;
         cout << recvbuf << endl;



    } while(true);

 //------------------------------------------
    closesocket(ConnectSocket);
    WSACleanup();
}

0 个答案:

没有答案