ok went back to working on my server side TCP/IP program.
so far I am able to create, listen, and connect from a client.
just having probs with what to do next.
my goal is the following:
1server running
2client connects
3server sends "8888" to show that it is connected and waiting to receive
4client sends a number to server
5server ack with sending "8888" for good, or "9999" for bad
then repeating 4-5, until client sends 0000 for exit
the following is the start of my program:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
//----- Defines ---------------------------------------------------------------
#define PORT_NUM 8888 // Arbitrary port number for the server
int main(void)
{
WSADATA wsaData; // Stuff for WSA functions
int welcome_s; // Welcome socket descriptor
struct sockaddr_in server_addr; // Server Internet address
int connect_s; // Connection socket descriptor
struct sockaddr_in client_addr; // Client Internet address
struct in_addr client_ip_addr; // Client IP address
int addr_len; // Internet address length
char out_buf[8]; // Output buffer for data
char in_buf[8]; // Input buffer for data
int retcode; // Return code
int number;
// This stuff initializes winsock
if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
{
fprintf(stderr,"Server: WSAStartup failed.\n", WSAGetLastError());
}
else
{
printf("Server: WSAStartup Success.\n");
}
// >>> Step #1 <<<
// Create a welcome socket
// - AF_INET is Address Family Internet and SOCK_STREAM is streams
welcome_s = socket(AF_INET, SOCK_STREAM, 0);
if (welcome_s < 0)
{
fprintf(stderr,"Server: Could not create socket.\n", WSAGetLastError());
}
else
{
printf("Server: Socket Created.\n");
}
// >>> Step #2 <<<
// Fill-in server (my) address information and bind the welcome socket
memset((char *)&server_addr,0,sizeof(server_addr)); //Clear server address
server_addr.sin_family = AF_INET; // Address family to use
server_addr.sin_port = htons(PORT_NUM); // Port number to use
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on any IP address
if (bind(welcome_s, (struct sockaddr *)&server_addr,sizeof(server_addr)) < 0)
{
fprintf(stderr,"Server: bind() failed with error %d\n", WSAGetLastError());
}
else
{
printf("Server: Socket bound.\n");
}
// >>> Step #3 <<<
// Listen on welcome socket for a connection
listen(welcome_s, 1);
// >>> Step #4 <<<
// Accept a connection. The accept() will block and then return with
// connect_s assigned and client_addr filled-in.
printf("Waiting for accept() to complete... \n");
addr_len = sizeof(client_addr);
connect_s = accept(welcome_s, (struct sockaddr *)&client_addr, &addr_len);
if (connect_s < 0)
{
fprintf(stderr,"Server: listen() failed with error %d\n", WSAGetLastError());
}
else
{
printf("Server: listen() is OK.\n");
}
// Copy the four-byte client IP address into an IP address structure
memcpy(&client_ip_addr, &client_addr.sin_addr.s_addr, 4);
// Print an informational message that accept completed
printf("Accept completed (IP address of client = %s port = %d) \n",
inet_ntoa(client_ip_addr), ntohs(client_addr.sin_port));
where do I go with this now?
thank you