|
Develop a simple chat application consisting of a client and a server. HELP MEEEEEEE
Develop a simple chat application consisting of a client and a server. These two applications communicate using socket on TCP. I HAVE THE CLIENT AND SERVER PROGRAM, I JUST NEED TO MODIFY THEM TO DO THIS PROJECT. CAN SOMEBODY HELP ME PLEASE.
HERE IT IS THE CLIENT PROGRAM:
//
// DClient.cpp
//
// Extremely simple, totally useless datagram client example.
// Works in conjunction with DServer.cpp.
//
// The program attempts to connect to the server and port
// specified on the command line. The DServer program prints
// the needed information when it is started. Once connected,
// the program sends data to the server, waits for a response
// and then exits.
//
// Compile and link with wsock32.lib
//
// Pass the server name and port number on the command line.
//
// Example: DClient MyMachineName 2000
//
#include <stdio.h>
#include <string.h>
#include <winsock.h>
// Function prototype
void DatagramClient(char *szServer, short nPort);
// Helper macro for displaying errors
#define PRINTERROR(s) \
fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())
////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
short nPort;
//
// Check for the host and port arguments
//
if (argc != 3)
{
fprintf(stderr,"\nSyntax: dclient ServerName PortNumber\n");
system("pause");
return 0;
}
nPort = atoi(argv[2]);
//
// Initialize WinSock and check the version
//
nRet = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
fprintf(stderr,"\n Wrong version\n");
system("pause");
return 0;
}
//
// Go do all the stuff a datagram client does
//
DatagramClient(argv[1], nPort);
system("pause");
//
// Release WinSock
//
WSACleanup();
}
////////////////////////////////////////////////////////////
void DatagramClient(char *szServer, short nPort)
{
printf("\nDatagram Client sending to server: %s on port: %d",
szServer, nPort);
//
// Find the server
//
LPHOSTENT lpHostEntry;
lpHostEntry = gethostbyname(szServer);
if (lpHostEntry == NULL)
{
PRINTERROR("gethostbyname()");
return;
}
//
// Create a UDP/IP datagram socket
//
SOCKET theSocket;
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
//
SOCKADDR_IN saServer;
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
//
char szBuf[256];
int nRet;
strcpy(szBuf, "From the Client");
nRet = sendto(theSocket, // Socket
szBuf, // Data buffer
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));
int nFromLen;
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
//
printf("\nData received: %s", szBuf);
closesocket(theSocket);
return;
}
HERE IT IS THE SERVER PROGRAM:
//
// DServer.cpp
//
// Extremely simple, totally useless datagram server example.
// Works in conjunction with DClient.cpp.
//
// The program sets itself up as a server using the UDP
// protoocl. It waits for data from a client, displays
// the incoming data, sends a message back to the client
// and then exits.
//
// Compile and link with wsock32.lib
//
// Pass the port number that the server should bind() to
// on the command line. Any port number not already in use
// can be specified.
//
// Example: DServer 2000
//
#include <stdio.h>
#include <winsock.h>
// Function prototype
void DatagramServer(short nPort);
// Helper macro for displaying errors
#define PRINTERROR(s) \
fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())
////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
short nPort;
//
// Check for port argument
//
if (argc != 2)
{
fprintf(stderr,"\nSyntax: dserver PortNumber\n");
system("pause");
return 0;
}
nPort = atoi(argv[1]);
//
// Initialize WinSock and check version
//
nRet = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
fprintf(stderr,"\n Wrong version\n");
system("pause");
return 0;
}
//
// Do all the stuff a datagram server does
//
DatagramServer(nPort);
//
// Release WinSock
//
WSACleanup();
}
////////////////////////////////////////////////////////////
void DatagramServer(short nPort)
{
//
// Create a UDP/IP datagram socket
//
SOCKET theSocket;
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
//
SOCKADDR_IN saServer;
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
//
int nRet;
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 where the server is waiting
// so that you can connect the example client.
//
int nLen;
nLen = sizeof(SOCKADDR);
char szBuf[256];
nRet = gethostname(szBuf, sizeof(szBuf));
if (nRet == SOCKET_ERROR)
{
PRINTERROR("gethostname()");
closesocket(theSocket);
return;
}
//
// Show the server name and port number
//
printf("\nServer named %s waiting on port %d\n",
szBuf, nPort);
//
// Wait for data from the client
//
SOCKADDR_IN saClient;
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
//
printf("\nData received: %s", szBuf);
//
// Send data back to the client
//
strcpy(szBuf, "From the Server");
sendto(theSocket, // Bound socket
szBuf, // Send buffer
strlen(szBuf), // Length of data to be sent
0, // Flags
(LPSOCKADDR)&saClient, // Address to send data to
nLen); // Length of address
//
// Normally a server continues to run so that
// it is available to other clients. In this
// example, we exit as soon as one transaction
// has taken place.
//
closesocket(theSocket);
return;
}
Last edited by admin II : 11-Mar-2007 at 15:59.
Reason: Please surround your C code with [cpp] ... [/cpp]
|