   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.#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() functionvoid shutdownServer(int socket);int shut_down = 0;char sendbuff[32] = "";char sendoksize[32];int main() {// the socket my server will use for listeningint serverSocket;// startup my network utilities with my handy functionsserverSocket = startupServerForListening(8000);// check for errorsif (serverSocket == -1) {printf("Network Startup Failed!\nProgram Terminating\n");return -1;}// accept a clientint clientSocket;clientSocket = accept(serverSocket, 0, 0);// check for errorsif (clientSocket == SOCKET_ERROR) {printf("Accept Failed!\n");}// the number of bytes I send/read ... will also serve as my error codeint 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 linewhile(!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 readnBytes = recv(clientSocket, buffer, messageSize, 0);// check for errorsif (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 errorsint error;// the winsock data structureWSAData wsaData;// startup winsockif ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) {printf("Could Not Start Up Winsock!\n");return -1;}// create my socketint mySocket = socket(AF_INET, SOCK_STREAM, 0);// make sure nothing bad happenedif (mySocket == SOCKET_ERROR) {printf("Error Opening Socket!\n");return -1;}// the address structurestruct sockaddr_in server;// fill the address structure with appropriate dataserver.sin_family = AF_INET;server.sin_port = htons(port);server.sin_addr.s_addr = INADDR_ANY;// and now bind my socketif (bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {printf("Bind Failed!\n");closesocket(mySocket);return -1;}// mark my socket for listeningif (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 winsockvoid shutdownServer(int socket) {// close our socketclosesocket(socket);// shut down winsockWSACleanup();printf("Server Shutdown\n");}
  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!   ;  As jwalsh said, basicly, in ur implementation,when the client uses send, he will also have to make a call to recv info on the same socket (depending on the message).the server just sends the message through the clientSocket socket.   