📄 sockspx.c
字号:
break;
printf("recv() failed: %d\n", WSAGetLastError());
return -1;
}
if (ret == 0)
break;
iTotal += ret;
iLeft -= ret;
}
return iTotal;
}
//
// SendDatagram() is generic rotuine to send a datagram to a
// specifid host.
//
int SendDatagram(SOCKET s, char *pchBuffer, SOCKADDR_IPX *psa)
{
int ret;
ret = sendto(s, pchBuffer, strlen ( pchBuffer ), 0,
(SOCKADDR *)psa, sizeof(SOCKADDR_IPX ));
if (ret == SOCKET_ERROR)
{
printf("sendto() failed: %d\n", WSAGetLastError());
return -1;
}
return ret;
}
//
// ReceiveDatagram() is generic rotuine to receive a datagram from a
// specifid host.
//
int ReceiveDatagram(SOCKET s, char *pchBuffer, SOCKADDR_IPX *psa,
int *pcb)
{
int ret;
ret = recvfrom(s, pchBuffer, MAX_DATA_LEN, 0, (SOCKADDR *) psa,
pcb);
if (ret == SOCKET_ERROR)
{
printf("recvfrom() failed: %d\n", WSAGetLastError());
return -1;
}
return ret;
}
//
// Server () performs the connection-less/connection-oriented server
// related tasks
//
void Server()
{
SOCKADDR_IPX sa_ipx, // Server address
sa_ipx_client; // Client address
char chBuffer[MAX_DATA_LEN]; // Data buffer
int ret,
cb;
CreateSocket ( );
//
// Bind to a local address and endpoint
//
BindSocket(&sa_ipx, pszLocalAddress, pszServerEndpoint);
//
// Check the Specified protocol. Call listen(), accept() if a connection
// oriented protocol is specified
//
if (chProtocol != 'd')
{
ret = listen(sock, 5);
if (ret == SOCKET_ERROR)
{
printf("listen() failed: %d\n", WSAGetLastError());
return;
}
printf("Waiting for a Connection...\n");
//
// Wait for a connection
//
while (1)
{
cb = sizeof(sa_ipx_client);
newsock = accept(sock, (SOCKADDR *) &sa_ipx_client, &cb);
if (newsock == INVALID_SOCKET)
{
printf("accept() failed: %d\n", WSAGetLastError());
return;
}
// Print the address of connected client
//
printf("Connected to Client Address - ");
PrintIpxAddress(sa_ipx_client.sa_netnum, sa_ipx_client.sa_nodenum);
while (1)
{
// Receive data on newly created socket
//
ret = ReceiveData(newsock, chBuffer);
if (ret == 0)
break;
else if (ret == -1)
return;
//
// Print the contents of received data
//
chBuffer[ret] = '\0';
printf("%d bytes of data received->%s\n", ret, chBuffer);
//
// Send data on newly created socket
//
ret = SendData(newsock, chBuffer);
if (ret == 0)
break;
else if (ret == -1)
return;
printf("%d bytes of data sent\n", ret);
}
closesocket(newsock);
}
}
else // Server will recv and send datagrams
{
// Receive a datagram on the bound socket
//
while (1)
{
cb = sizeof (sa_ipx_client);
ret = ReceiveDatagram(sock, chBuffer, &sa_ipx_client, &cb);
if (ret == -1)
return;
//
// Print the contents of received datagram and the senders address
//
printf("Message Received from Client Address - ");
PrintIpxAddress( sa_ipx_client.sa_netnum, sa_ipx_client.sa_nodenum );
chBuffer[ret] = '\0';
printf("%d bytes of data received->%s\n", ret, chBuffer);
//
// Echo the datagram on the bound socket to the client
//
ret = SendDatagram(sock, chBuffer, &sa_ipx_client);
if (ret == -1)
return;
printf("%d bytes of data sent\n", ret);
}
closesocket(newsock);
}
return;
}
//
// Client () performs the connection-less/connection-oriented client
// related tasks
//
void Client()
{
SOCKADDR_IPX sa_ipx, // client address
sa_ipx_server; // server address
char chBuffer[MAX_DATA_LEN]; // data buffer
int ret,
cb;
DWORD i;
CreateSocket();
//
// Bind to a local address and endpoint
//
BindSocket(&sa_ipx, pszLocalAddress, NULL);
if (pszServerEndpoint == NULL)
{
printf("Server Endpoint must be specified....Exiting\n");
ExitProcess(-1);
}
// Fill the sa_ipx_server address address with server address and endpoint
//
if (pszServerAddress != NULL)
{
FillIpxAddress ( &sa_ipx_server, pszServerAddress, pszServerEndpoint);
}
else
{
printf("Server Address must be specified....Exiting\n");
ExitProcess(-1);
}
// Check the Specified protocol. Call connect() if a connection oriented
// protocol is specified
//
if (chProtocol != 'd')
{
printf("Connecting to Server -");
PrintIpxAddress ( sa_ipx_server.sa_netnum, sa_ipx_server.sa_nodenum );
//
// Connect to the server
//
ret = connect(sock, (SOCKADDR *) &sa_ipx_server, sizeof sa_ipx_server);
if (ret == SOCKET_ERROR)
{
printf("connect() failed: %d\n", WSAGetLastError());
return;
}
printf("Connected to Server Address - ");
PrintIpxAddress( sa_ipx_server.sa_netnum, sa_ipx_server.sa_nodenum );
//
// Send data to the specfied server
//
memset(chBuffer, '*', dwNumBytes);
chBuffer[dwNumBytes] = 0;
for(i=0; i < dwNumToSend ;i++)
{
ret = SendData(sock, chBuffer);
if (ret == 0)
return;
else if (ret == -1)
return;
printf("%d bytes of data sent\n", ret);
//
// Receive data from the server
//
ret = ReceiveData(sock, chBuffer);
if (ret == 0)
return;
else if (ret == -1)
return;
//
// Print the contents of received data
//
chBuffer[ret] = '\0';
printf("%d bytes of data received->%s\n", ret, chBuffer);
}
}
else
{
// Send a datagram to the specified server
//
memset(chBuffer, '*', dwNumBytes);
chBuffer[dwNumBytes] = 0;
for(i=0; i < dwNumToSend ;i++)
{
ret = SendDatagram(sock, chBuffer, &sa_ipx_server);
if (ret == -1)
return;
printf("%d bytes of data sent\n", ret);
//
// Receive a datagram from the server
//
cb = sizeof(sa_ipx_server);
ret = ReceiveDatagram(sock, chBuffer, &sa_ipx_server, &cb);
if (ret == -1)
return;
//
// Print the contents of received data
//
chBuffer[ret] = '\0';
printf("%d bytes of data received->%s\n", ret, chBuffer);
}
}
return;
}
//
// Function: CheckProtocol
//
// Description:
// CheckProtocol() checks if a valid protocol is specified on
// the command line.
//
BOOL CheckProtocol(char chProtocol)
{
if (('d' != chProtocol) && ('s' != chProtocol) &&
('p' != chProtocol))
{
return FALSE;
}
return TRUE;
}
//
// Function: ValidateArgs
//
// Description:
// Parses the command line arguments and sets some global
// variables to determine the behavior of the application.
//
void ValidateArgs(int argc, char **argv)
{
int i;
for (i=1; i < argc ;i++)
{
if ((argv[i][0] == '-') || (argv[i][0] == '/' ))
{
switch (tolower (argv[i][1]))
{
case 's': // act as server
bServer = TRUE;
break;
case 'c': // act as client
bServer = FALSE;
break;
case 'e': // server endpoint
pszServerEndpoint = &argv[i][3];
break;
case 'n': // server address
pszServerAddress = &argv[i][3];
break;
case 'l': // local address
pszLocalAddress = &argv[i][3];
break;
case 'm': // enumerate local addrs
bEnumerate = TRUE;
break;
case 'p': // protocol
chProtocol = tolower(argv[i][3]);
if (CheckProtocol(chProtocol) == FALSE)
{
printf("Unknown protcol specified\n\n");
usage(argv[0]);
}
break;
case 'b': // number of bytes to send
dwNumBytes = atoi(&argv[i][3]);
break;
case 'r': // number of repititions
dwNumToSend = atoi(&argv[i][3]);
break;
default:
usage(argv[0]);
break;
}
}
else
usage(argv[0]);
}
return;
}
//
// Function: main
//
// Description:
// The main function which loads Winsock, parses the command line,
// and initiates either the client or server routine depending
// on the parameters passed.
//
int main(int argc, char **argv)
{
WSADATA wsd;
ValidateArgs(argc, argv);
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
printf("WSAStartup() failed: %d\n", GetLastError());
return -1;
}
// Check to see if the role of the application is to enumerate local
// adapters
//
if (bEnumerate)
EnumerateAdapters();
else
{
if (bServer) // Act as server
Server();
else // Act as client
Client();
}
closesocket(sock);
closesocket(newsock);
WSACleanup();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -