#include <stdio.h>
#include <winsock2.h>
#define MAX_MESSAGE_SIZE 4096
// -----------------------------------------------------------------------------------
// startupServerForListening() - will return us a socket that is bound to the
// port we specify, and is listening for connections if all operations succeeded,
// and a -1 if startup failed.
int startupServerForListening(unsigned short port);
// -----------------------------------------------------------------------------------
// shutdownServer() - will take the socket we specify and shutdown the
// network utilities we started with startupServerForListening()
// Note: In order to function properly, the socket passed in MUST be the
// socket created by the startupServerForListening() function
void shutdownServer(int socket);
int shut_down = 0;
char sendbuff[32] = "";
char sendoksize[32];
int main() {
// the socket my server will use for listening
int serverSocket;
// startup my network utilities with my handy functions
serverSocket = startupServerForListening(8000);
// check for errors
if (serverSocket == -1) {
printf("Network Startup Failed!\nProgram Terminating\n");
return -1;
}
// accept a client
int clientSocket;
clientSocket = accept(serverSocket, 0, 0);
// check for errors
if (clientSocket == SOCKET_ERROR) {
printf("Accept Failed!\n");
}
// the number of bytes I send/read ... will also serve as my error code
int nBytes;
// We need a big enough buffer for our maximum message size.
char buffer[MAX_MESSAGE_SIZE];
// And we need a variable for our size.
unsigned long messageSize=MAX_MESSAGE_SIZE;
//telnet sends one character at a time, so we loop to get a whole line
while(!shut_down)
{
char c=' ';
char message[MAX_MESSAGE_SIZE];
int count=0;
do
{
// And now receive the rest of the message.
// read up to messageSize bytes into buffer
//and return the number of bytes read
nBytes = recv(clientSocket, buffer, messageSize, 0);
// check for errors
if (nBytes == SOCKET_ERROR)
{
printf("Recv Failed!\n");
}
c=buffer[0];
message[count] = c;
count++;
}
while((nBytes >0) && (c!='\n') && (c!='\r'));
message[count]='\0';
// At this point we should have received data from clientSocket, and stored it in inMessage.
// So lets print out our input buffer and see what the message was ...
printf("Message Received : %s\n", message);
if((message[0] == 'q') && (message[1] == 'u') && (message[2] == 'i') && (message[3] == 't'))
{
printf("Logging off...\n");
shut_down = 1;
}
}
closesocket(clientSocket);
shutdownServer(serverSocket);
printf("Press any key to continue ...\n");
getchar();
}
int startupServerForListening(unsigned short port) {
// an error code we will use to get more information about our errors
int error;
// the winsock data structure
WSAData wsaData;
// startup winsock
if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) {
printf("Could Not Start Up Winsock!\n");
return -1;
}
// create my socket
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
// make sure nothing bad happened
if (mySocket == SOCKET_ERROR) {
printf("Error Opening Socket!\n");
return -1;
}
// the address structure
struct sockaddr_in server;
// fill the address structure with appropriate data
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = INADDR_ANY;
// and now bind my socket
if (bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
printf("Bind Failed!\n");
closesocket(mySocket);
return -1;
}
// mark my socket for listening
if (listen(mySocket, 5) == SOCKET_ERROR) {
printf("Listen Failed!\n");
closesocket(mySocket);
return -1;
}
printf("Server Started\n");
return mySocket;
}
// -----------------------------------------------------------------------------------
// shutdownServer() - a function to shutdown a socket and clean up winsock
void shutdownServer(int socket) {
// close our socket
closesocket(socket);
// shut down winsock
WSACleanup();
printf("Server Shutdown\n");
}
send and recv question
I have a simple server here that waits for a connection, and then echos what is typed in, and is supposed to also echo what the client types back to them. The server accepts the clients via telnet. Problem is i cannot figure out how to get the server to send anything to the client. Hopefully someone can help me here.
Talith,
You send to the client the same way the client sent to the server.
Since you're using a connected stream you just call the "send()" function with the socket which is connected to the client.
Additional parameters include: the buffer of data to send (likely the same thing you're sending to the screen), the length of the buffer, and any flags you desire. If you're planning to do something more complicated with the program you might want to send along to the client a small header identifying what kind of information its about to receive so it knows what to do with it, in this case, that its going to be a string of input characters to by echod to the screen. But for a simple echo program, this isnt necessary, so long as the client knows what to do with the input when it receives it.
Here's the decleration of the send function:
int send( SOCKET s, const char* buf, int len, int flags );
You can find more info about it on MSDN Here.
Cheers!
You send to the client the same way the client sent to the server.
Since you're using a connected stream you just call the "send()" function with the socket which is connected to the client.
Additional parameters include: the buffer of data to send (likely the same thing you're sending to the screen), the length of the buffer, and any flags you desire. If you're planning to do something more complicated with the program you might want to send along to the client a small header identifying what kind of information its about to receive so it knows what to do with it, in this case, that its going to be a string of input characters to by echod to the screen. But for a simple echo program, this isnt necessary, so long as the client knows what to do with the input when it receives it.
Here's the decleration of the send function:
int send( SOCKET s, const char* buf, int len, int flags );
You can find more info about it on MSDN Here.
Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement