sockets programming [Archive] - C Board

PDA

View Full Version : sockets programming


kaijuu
03-21-2008, 11:25 AM
hi i have a small problem this is the code i was given for creating a socket

if (argc != 2) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]);
exit(1);
}

echoServPort = atoi(argv[1]); /* First arg: local port */



/* Create socket for incoming connections */
if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket() failed");

/* Construct local address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
echoServAddr.sin_port = htons(echoServPort); /* Local port */

/* Bind to the local address */
if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("bind() failed");

/* Mark the socket so it will listen for incoming connections */
if (listen(servSock, MAXPENDING) < 0)
DieWithError("listen() failed");

for (;;) /* Run forever */
{
/* Set the size of the in-out parameter */
clntLen = sizeof(echoClntAddr);

/* Wait for a client to connect */
if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr,
&clntLen)) < 0)
DieWithError("accept() failed");

/* clntSock is connected to a client! */

printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));

HandleTCPClient(clntSock);
}
/* NOT REACHED */
}

this will ask a user for ip address, socket and message and send to a client (though im really not 100% sure how)

this is the bit i need to change if i can

if (argc != 2) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]);
exit(1);
}

you see i dont want it to ask for addresses, ports and messages i would like it to ask for a client name eg "R1" then conect to a predeteurmend ipaddress and port and then ask for a string (open or close) to be sent across the socket

can any body tell me how to do this and also can i put the names and ip addresses in an array

And finaly im i barking up the wrong tree
hope you can help

rags_to_riches
03-21-2008, 12:18 PM
That's code for a basic TCP-based server. It's not going to send anything. It's going to wait for clients to connect to the port on which it's listening, then do whatever HandleTCPClient() does with the socket it's provided.

Here one good site for sockets development (http://beej.us/guide/bgnet/)
And here's another (http://members.aol.com/DSC30574/sockets/index.html)

kaijuu
03-23-2008, 05:58 PM
hi a different problem i need the server to initiate the connection with the server all the code ive seen has the client conect to the server how to i do this

vart
03-24-2008, 01:08 AM
so the server initiating connection will work as a client in the particular session

kaijuu
03-24-2008, 10:00 AM
k sorry i dont quite understand this bu im tring

will this work

have all ten of my clients keep trying to set up a connection and have my server only alow the connection to the one it needs to send a messasge to ?

that way i can keep sending messages to different clients using a simple loop

brewbuck
03-24-2008, 10:04 AM
have all ten of my clients keep trying to set up a connection and have my server only alow the connection to the one it needs to send a messasge to ?

How does it know which client is the one it needs? I assume you would determine this by checking the client IP address. Ignoring the fact that this is technically flawed in a NAT environment, it's also impossible, because the only way to get the client address is to accept() the connection. So you'd be sitting in a loop repeatedly accept()'ing, checking the IP, and close()'ing the clients you don't want.

No offense, but I think that sucks :)

kaijuu
03-24-2008, 11:52 AM
ok i better explain what im tring to do here

i have 10 clients and 1 server and im using sockets to connect them

imagin them like fingers.

my server asks which client the user would like to connect to and when this string is entered for example right 1, so the server will read a txt file to get the ip address for right 1.

the server will then connect to the ip address for right 1, and then ask the user for a command (open or close), which will be sent to the client.

the client will then open a small picture on to the client screen (a finger)

this is all done in a loop with errors

however i also need the client to connect to the server to send an ow message this will tell the server to send a close message to all or the right hand or left one (small loop)

i keep having problems with this and its genraly bothering me

brewbuck
03-24-2008, 12:30 PM
my server asks which client the user would like to connect to

By definition, clients connect to servers, not vice versa. So this already makes no sense. If your "server" process A is connecting to a "client" B, then A is actually the client and B is the server.

kaijuu
03-24-2008, 04:18 PM
so can you sugesst a way to get similer results in a different way then

manav
03-24-2008, 11:07 PM
don't assume that 1 is server and other 10 are clients. let them all be both (client and server).