您的位置首页生活快答

vc最简单的 网络编程如何在一台电脑测试

vc最简单的 网络编程如何在一台电脑测试

的有关信息介绍如下:

vc最简单的 网络编程如何在一台电脑测试

利用回送IP:127.0.0.1 开两个程序,一个server,一个client

如MSDN实例:

client端:

#include

#include "winsock2.h"

void main() {

// Initialize Winsock.

WSADATA wsaData;

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

if ( iResult != NO_ERROR )

printf("Error at WSAStartup()\n");

// Create a socket.

SOCKET m_socket;

m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( m_socket == INVALID_SOCKET ) {

printf( "Error at socket(): %ld\n", WSAGetLastError() );

WSACleanup();

return;

}

// Connect to a server.

sockaddr_in clientService;

clientService.sin_family = AF_INET;

clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );

clientService.sin_port = htons( 27015 );

if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {

printf( "Failed to connect.\n" );

WSACleanup();

return;

}

// Send and receive data.

int bytesSent;

int bytesRecv = SOCKET_ERROR;

char sendbuf = "Client: Sending data.";

char recvbuf = "";

bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );

printf( "Bytes Sent: %ld\n", bytesSent );

while( bytesRecv == SOCKET_ERROR ) {

bytesRecv = recv( m_socket, recvbuf, 32, 0 );

if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {

printf( "Connection Closed.\n");

break;

}

if (bytesRecv < 0)

return;

printf( "Bytes Recv: %ld\n", bytesRecv );

}

return;

}

server端:

#include

#include "winsock2.h"

void main() {

// Initialize Winsock.

WSADATA wsaData;

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

if ( iResult != NO_ERROR )

printf("Error at WSAStartup()\n");

// Create a socket.

SOCKET m_socket;

m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( m_socket == INVALID_SOCKET ) {

printf( "Error at socket(): %ld\n", WSAGetLastError() );

WSACleanup();

return;

}

// Bind the socket.

sockaddr_in service;

service.sin_family = AF_INET;

service.sin_addr.s_addr = inet_addr( "127.0.0.1" );

service.sin_port = htons( 27015 );

if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) {

printf( "bind() failed.\n" );

closesocket(m_socket);

return;

}

// Listen on the socket.

if ( listen( m_socket, 1 ) == SOCKET_ERROR )

printf( "Error listening on socket.\n");

// Accept connections.

SOCKET AcceptSocket;

printf( "Waiting for a client to connect...\n" );

while (1) {

AcceptSocket = SOCKET_ERROR;

while ( AcceptSocket == SOCKET_ERROR ) {

AcceptSocket = accept( m_socket, NULL, NULL );

}

printf( "Client Connected.\n");

m_socket = AcceptSocket;

break;

}

// Send and receive data.

int bytesSent;

int bytesRecv = SOCKET_ERROR;

char sendbuf = "Server: Sending Data.";

char recvbuf = "";

bytesRecv = recv( m_socket, recvbuf, 32, 0 );

printf( "Bytes Recv: %ld\n", bytesRecv );

bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );

printf( "Bytes Sent: %ld\n", bytesSent );

return;

}

配个虚拟机,比如vmware virtual box之类的。

楼上V5

要测试功能的话方法1就行了,要进行网络测试的话还是组个小网络吧

方法1:

都用IP 127.0.0.1

方法2:

配个虚拟机,比如vmware virtual box之类的。

推荐方法2.方法1太假了。