简体   繁体   English

使用C ++在Visual Studio中进行套接字编程

[英]Socket Programming in visual studio with C++

I write a program for chat, but when i run server side,it produce error in listen function. 我编写了一个聊天程序,但是当我运行服务器端时,它会在侦听功能中产生错误。 i don't know what's the causes error! 我不知道是什么原因导致的错误! by the way, how i must run that?first server then client?what about IP address? 顺便说一句,我必须如何运行?首先是服务器,然后是客户端?IP地址又如何? how i set them? 我如何设置它们? i want to run it just on the same PC. 我想只在同一台PC上运行它。 ` `

    #include "stdafx.h"
    #include <winsock2.h>
    #include <Ws2tcpip.h>
    #include <stdio.h>
    #include "iostream"
    #include "conio.h"
    using namespace std;
   #pragma comment(lib, "Ws2_32.lib")
   #define WIN32_LEAN_AND_MEAN
   #define DEFAULT_BUFLEN 512
   #define DEFAULT_PORT 27015

int main()
{

WSADATA wsaData;
int iResult = 0;            
int recvbuflen = DEFAULT_BUFLEN;
char *sendbuf = "Server: sending data test";
char recvbuf[DEFAULT_BUFLEN] = "";


SOCKET ListenSocket = INVALID_SOCKET;


sockaddr_in service;


iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
    cout<<"Error at WSAStartup()\n";

}

ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET) {
    cout<<"socket function failed with error: %u\n"<< WSAGetLastError();
    WSACleanup();
    getch();

}

service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("192.168.1.52");
service.sin_port = htons(27015);

iResult = bind(ListenSocket, (SOCKADDR *) &service, sizeof (service));
if (iResult == SOCKET_ERROR) {
    cout<<"bind failed with error \n"<< WSAGetLastError();
    getch();
    closesocket(ListenSocket);
    WSACleanup();

}
else{

    wprintf(L"bind returned success\n");
    WSACleanup();
    getch();
}

if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR){
    cout<<"listen function failed with error: "<< WSAGetLastError();
    iResult = closesocket(ListenSocket);
    WSACleanup();
    getch();
}



cout<<"Waiting for client to connect...\n";

ListenSocket = accept(ListenSocket, NULL, NULL);
if (ListenSocket == INVALID_SOCKET) {
    cout<<"accept failed with error: \n"<< WSAGetLastError();
    closesocket(ListenSocket);
    WSACleanup();
    getch();
} else
    cout<<"Client connected.\n";

iResult = send( ListenSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
    cout<<"send failed with error: "<< WSAGetLastError();
    closesocket(ListenSocket);
    getch();
    WSACleanup();

}else
    cout<<"Bytes Sent: "<< iResult;


do {

    iResult = recv(ListenSocket, recvbuf, recvbuflen, 0);
    if ( iResult > 0 )
        cout<<"Bytes received: "<< iResult;   
    else if ( iResult == 0 ){
        cout<<"Connection closed";   
    getch();}
    else{
        cout<<"recv failed with error: "<< WSAGetLastError();
       getch();
    }
} while( iResult > 0 );


iResult = closesocket(ListenSocket);
if (iResult == SOCKET_ERROR) {
    cout<<"close failed with error: \n"<< WSAGetLastError();
    WSACleanup();
    getch();
}


return 0;
    }

` `

#include "stdafx.h"
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include "iostream"
#include "conio.h"
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#define WIN32_LEAN_AND_MEAN
#define DEFAULT_BUFLEN 512
 #define DEFAULT_PORT 27015

int main() {

int Result;
WSADATA wsaData;

SOCKET SocketNumber = INVALID_SOCKET;
struct sockaddr_in clientService; 

int recvbuflen = DEFAULT_BUFLEN;
char *sendbuf = "Client: sending data test";
char recvbuf[DEFAULT_BUFLEN] = "";

Result = WSAStartup(MAKEWORD(2,2), &wsaData);
if (Result != NO_ERROR) {
    cout<<"WSAStartup failed with error: "<< Result;
    getch();
}

SocketNumber = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (SocketNumber == INVALID_SOCKET) {
    cout<<"socket failed with error: "<< WSAGetLastError();
       getch();
    WSACleanup();
}

clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
clientService.sin_port = htons( DEFAULT_PORT );

Result = connect( SocketNumber, (SOCKADDR*) &clientService, sizeof(clientService) );
if (Result == SOCKET_ERROR) {
    cout<<"connect failed with error: "<< WSAGetLastError();
    closesocket(SocketNumber);
    getch();
    WSACleanup();

 }
Result = send( SocketNumber, sendbuf, (int)strlen(sendbuf), 0 );
if (Result == SOCKET_ERROR) {
    cout<<"send failed with error: "<< WSAGetLastError();
    closesocket(SocketNumber);
    getch();
    WSACleanup();

}

cout<<"Bytes Sent: "<< Result;

Result = shutdown(SocketNumber, SD_SEND);
if (Result == SOCKET_ERROR) {
    cout<<"shutdown failed with error: "<< WSAGetLastError();
    closesocket(SocketNumber);
       getch();
    WSACleanup();

}
do {

    Result = recv(SocketNumber, recvbuf, recvbuflen, 0);
    if ( Result > 0 )
        cout<<"Bytes received: "<< Result;   
    else if ( Result == 0 )
        cout<<"Connection closed";   
    else
        cout<<"recv failed with error: "<< WSAGetLastError();
       getch();
} while( Result > 0 );


Result = closesocket(SocketNumber);
if (Result == SOCKET_ERROR) {
    wprintf(L"close failed with error: %d\n", WSAGetLastError());
       getch();
    WSACleanup();
    return 1;
}

WSACleanup();

return 0;
}

delete two lines after " wprintf(L"bind returned success\\n");" 删除“ wprintf(L” bind返回成功\\ n“);”之后的两行 in server, i mean these lines: 在服务器中,我的意思是这些行:

WSACleanup();
getch();

and it will be ok! 没关系!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM