-
February 25th, 2013, 02:38 PM
#1
simple client server app
I have a client \ server project that I'm working on. Very entry level:
I have to do the following
- Server- Code in the listening port and start the server (I assume by executing the program...???)
- Client - Enter code that prompts the user to add the ip address of the server and the port to be connected to
- Upon entering that info- I assume, it will spit back a server connection verification to the client program.
I have both the client code and the server code that I need to edit to obtain the desired results, but not sure where to look to do this:
SERVER:
HTML Code:
#include <iostream>
using namespace std;
//#include <stdio.h>
#include <winsock.h>
#include <time.h>
#include <string.h>
// Function prototypes
void DatagramServer(short nPort);
void gettime( char tstr[], char server[] );
// Helper macro for displaying errors
#define PRINTERROR(s) cout << endl << s << ": " << WSAGetLastError() << endl;
int main(int argc, char **argv)
{
WORD wVersionRequested = MAKEWORD(1,1); // Winsock version negotiation
WSADATA wsaData; // WSAData data structure
int nRet; // return code
short nPort; // port number
//
// Check for port argument
//
if (argc != 2)
{
cout << "\nSyntax: Timesrvr PortNumber\n";
return 0;
}
nPort = atoi(argv[1]);
//
// Initialize WinSock and check version
//
nRet = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
cout << "\nIncorrect WSA version... abort\n";
return 0;
}
//
// Do all the stuff a UDP datagram server does
//
DatagramServer(nPort);
//
// Release WinSock
//
WSACleanup();
}
////////////////////////////////////////////////////////////
void DatagramServer(short nPort)
{
SOCKET theSocket; // Server Socket
SOCKADDR_IN saServer; // Server details
SOCKADDR_IN saClient; // Client details
int nRet; // return code
int nLen; // buffer length
char szBuf[256]; // buffer space
char szSvr[256]; // buffer space
//
// Create a UDP/IP datagram socket
//
theSocket = socket(AF_INET, // Address family
SOCK_DGRAM, // Socket type
IPPROTO_UDP);// Protocol
if (theSocket == INVALID_SOCKET)
{
PRINTERROR("socket()");
return;
}
//
// Fill in the address structure
//
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = INADDR_ANY; // Let WinSock assign address
saServer.sin_port = htons(nPort); // Use port passed from user
//
// bind the name to the socket
//
nRet = bind(theSocket, // Socket descriptor
(LPSOCKADDR)&saServer, // Address to bind to
sizeof(struct sockaddr) // Size of address
);
if (nRet == SOCKET_ERROR)
{
PRINTERROR("bind()");
closesocket(theSocket);
return;
}
//
// This isn't normally done or required, but in this
// example we're printing out the port where the server
// is waiting so that you can connect the example client.
//
nLen = sizeof(SOCKADDR);
nRet = gethostname(szSvr, sizeof(szSvr));
if (nRet == SOCKET_ERROR)
{
PRINTERROR("gethostname()");
closesocket(theSocket);
return;
}
//
// Show the server name and port number
//
cout << "\nServer [" << szSvr << "] waiting on port " << nPort << endl;
//
// Wait for data from the client
//
do
{
memset(szBuf, 0, sizeof(szBuf));
nRet = recvfrom(theSocket, // Bound socket
szBuf, // Receive buffer
sizeof(szBuf), // Size of buffer in bytes
0, // Flags
(LPSOCKADDR)&saClient, // Buffer to receive client address
&nLen); // Length of client address buffer
//
// Show that we've received some data
//
cout << "\nData received: " << szBuf << endl;
//
// Get the system time
//
gettime(szBuf, szSvr);
//
// Send data back to the client
//
sendto(theSocket, // Bound socket
szBuf, // Send buffer
(int)strlen(szBuf), // Length of data to be sent
0, // Flags
(LPSOCKADDR)&saClient, // Address to send data to
nLen); // Length of address
} while (TRUE);
//
// And exit if user hits Control-C
//
closesocket(theSocket);
return;
}
void gettime( char tstr[], char server[] )
{
struct tm newtime;
__time32_t aclock;
char buffer[32];
errno_t errNum;
_time32( &aclock ); // Get time in seconds.
_localtime32_s( &newtime, &aclock ); // Convert time to struct tm form.
// Print local time as a string.
errNum = asctime_s(buffer, 32, &newtime);
if (errNum)
{
printf("Error code returned from asctime_s function: %d", (int)errNum);
}
// Copy the result into the string to be returned
sprintf_s(tstr, 256, "From the [%s] Server: %s",
server, buffer );
}
CLIENT
HTML Code:
#include <iostream>
using namespace std;
#include <string.h>
#include <winsock.h>
// Function prototype
void DatagramClient(char *szServer, short nPort);
// Helper macro for displaying error messages
#define PRINTERROR(s) cout << endl << s << ": " << WSAGetLastError() << endl;
////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
WORD wVersionRequested = MAKEWORD(1,1); // WORD data type for Winsock version
WSADATA wsaData; // wsa data structure
int nRet; // return value
short nPort; // 16-bit port number
//
// Check for the host and port arguments
//
if (argc != 3)
{
cout << "Syntax: Timeclnt ServerName PortNumber\n\n";
return(0);
}
//
// extract the port number from the command line information
//
nPort = atoi(argv[2]);
//
// Initialize WinSock and check the version
//
nRet = WSAStartup(wVersionRequested, &wsaData);
//
// check for the proper return value
//
if (wsaData.wVersion != wVersionRequested)
{
cout << "Incorrect WSA version returned.\n";
return(0);
}
//
// Go do all the stuff a datagram client does
//
DatagramClient(argv[1], nPort);
//
// Clean up and release WinSock
//
WSACleanup();
}
////////////////////////////////////////////////////////////
void DatagramClient(char *szServer, short nPort)
{
char szBuf[256]; // data buffer
char szSvr[256]; // data buffer
int nRet; // return value
int nFromLen; // number of bytes returned
LPHOSTENT lpHostEntry; // LPHOSTENT network data structure
SOCKET theSocket; // Open the socket
SOCKADDR_IN saServer; // SOCKET address data structure: contains information about IP, ip address and the port.
//
// Get local machine name
//
nRet = gethostname(szSvr, sizeof(szSvr));
if (nRet == SOCKET_ERROR)
{
PRINTERROR("gethostname()");
return;
}
//
// output a little informational message
//
cout << endl << "Datagram Client [" << szSvr
<< "] sending to server ["
<< szServer << "] on port " << nPort << "...\n";
//
// Find the server
//
lpHostEntry = gethostbyname(szServer);
if (lpHostEntry == NULL)
{
PRINTERROR("gethostbyname()");
return;
}
//
// Create a UDP/IP datagram socket
//
theSocket = socket(AF_INET, // Address family
SOCK_DGRAM, // Socket type
IPPROTO_UDP); // Protocol
if (theSocket == INVALID_SOCKET)
{
PRINTERROR("socket()");
return;
}
//
// Fill in the address structure for the server
//
saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
// ^ Server's address
saServer.sin_port = htons(nPort); // Port number from command line
//
// Send data to the server
//
sprintf_s(szBuf, 256, "From the Client [%s]", szSvr);
nRet = sendto(theSocket, // Socket
szBuf, // Data buffer
(int)strlen(szBuf), // Length of data
0, // Flags
(LPSOCKADDR)&saServer, // Server address
sizeof(struct sockaddr)); // Length of address
if (nRet == SOCKET_ERROR)
{
PRINTERROR("sendto()");
closesocket(theSocket);
return;
}
//
// Wait for the reply
//
memset(szBuf, 0, sizeof(szBuf));
nFromLen = sizeof(struct sockaddr);
recvfrom(theSocket, // Socket
szBuf, // Receive buffer
sizeof(szBuf), // Length of receive buffer
0, // Flags
(LPSOCKADDR)&saServer, // Buffer to receive sender's address
&nFromLen); // Length of address buffer
if (nRet == SOCKET_ERROR)
{
PRINTERROR("recvfrom()");
closesocket(theSocket);
return;
}
//
// Display the data that was received
//
cout << szBuf << endl;
closesocket(theSocket);
return;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|