I am trying to create a client/server by C, this is my first C programing learning and I want to do connection by localhost only, not by internet , what am i missing. How do i fix it and get my server up and running, and what should i type to get my client connect to my server.
I need to create a server that can send and receive with client. Sorry i am new to C programing, just on learning phase, do pardon my lack of experience and would love to learn from experts here.
Below is my code in main
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
using namespace std;
int main()
{
int serverFd;
int clientFd;
struct sockaddr_un serverAddress;
cout << "" << endl;
cout << "Running server program 'css' ...... " << endl;
cout << "" << endl;
// SOCKET CREATION PART - SERVER
serverFd = socket (AF_LOCAL, SOCK_STREAM, 0);
/* Set domain type */
serverAddress.sun_family = AF_LOCAL;
/* Set name */
strcpy (serverAddress.sun_path, "Server");
/* Remove file if it already exists */
unlink ("Server");
/* Create file */
bind (serverFd, serverSockAddrPtr, serverLen);
// SOCKET CREATION END - SERVER
return 0;
}
Error I get:
CountryServer.c:39:17: error: ‘serverSockAddrPtr’ was not declared in this scope
CountryServer.c:39:36: error: ‘serverLen’ was not declared in this scope
As the errors say, you’re using names (
serverSockAddrPtrandserverLen) which you haven’t declared. What you want are the address and size of theserverAddressstructure: