-
Socket programming
Hi all,
I am comletely new in c programing,
I have a server client program in c , that I use thread in it,
Client send a sentence and server convert it to upper case and send to client,
My question is:
How can in server find which client connect to server, for example :
If a request receive from client 1 ,server send hello client 1 , and for client 2, it send hello client 2, and etc,
Very thanks for your helps,
-
If you're completely new to C, I would recommend starting with the basics, and tackling network and thread programming later on.
What type of sockets are you using?
Does your program use the "accept()" function?
If so, what can this tell you?
-
Socket Programming
Thanks dear,
My code in server is :
Code:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
int main(){
int welcomeSocket, newSocket, portNum, clientLen, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
int i;
welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
portNum = 7891;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(portNum);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
if(listen(welcomeSocket,5)==0)
printf("Listening\n");
else
printf("Error\n");
addr_size = sizeof serverStorage;
/*loop to keep accepting new connections*/
while(1){
newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
/*fork a child process to handle the new connection*/
if(!fork()){
nBytes = 1;
/*loop while connection is live*/
while(nBytes!=0){
nBytes = recv(newSocket,buffer,1024,0);
for (i=0;i<nBytes-1;i++){
buffer[i] = toupper(buffer[i]);
}
send(newSocket,buffer,nBytes,0);
}
close(newSocket);
exit(0);
}
/*if parent, close the socket and go back to listening new requests*/
else{
close(newSocket);
}
}
return 0;
}
-----------------------------------client code is
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int clientSocket, portNum, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;
clientSocket = socket(PF_INET, SOCK_STREAM, 0);
portNum = 7891;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(portNum);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
while(1){
printf("Type a sentence to send to server:\n");
fgets(buffer,1024,stdin);
printf("You typed: %s",buffer);
nBytes = strlen(buffer) + 1;
send(clientSocket,buffer,nBytes,0);
recv(clientSocket, buffer, 1024, 0);
printf("Received from server: %s\n\n",buffer);
}
return 0;
}
Thanks for your helps,
-
Quote:
Originally Posted by
Hamsaeed
My code in server is :
Your code? What you've posted is extremely similar to the code here: c - Client connects to server randomly - Stack Overflow
Furthermore, you've provided no answers to the questions I asked. There were clues in those questions that, if answered, might have proved useful.
If you're simply trying to hack away at copied code with none of the necessary knowledge, there is not much help I can give you.
Consider getting a good C programming book and starting at the very beginning. Maybe in a few months, you'll be ready to understand and write networking code that suits your purposes.
-
I am sorry,
in summery, my question is :
How to get information of client in server ?
-
Read post #2 again, and do a little bit of research.
-
Every time you accept a client, accept returns you a socket object which you can use to communicate with the client. After accepting a client, you should create a new thread to handle the communication between the server and that client. Now in order to be able to see all the clients on each thread, you need to create a list where you will store a reference of each socket object accept returns to you. So your code should look like this:
Code:
global list_of_clients;
void handleClient(client)
{
//do all the code for the client communication here.
//Now that you have a list with all the clients you can do this:
msg = recv(client) //Receive something from the client.
//Send the message which you received from current client
//to all the other clients except the one who send it.
for c in list_of_clients
{
if (c != client) send(msg, c);
}
}
void handleAccepts()
{
new_socket = accept();
list_of_clients.add( new_socket );
start_new_thread(handleClient, new_socket );
}
-
Ok and thanks for helpful answers,