Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Acknowledgement between client/server - C


  • Please log in to reply
3 replies to this topic

#1 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 13 December 2011 - 06:20 AM

Hello,

Trying to build a simple client/server program. Until know I achieved, connect client and server, sendto()-client and recvfrom()-server and disconnect them close(socket); My problem now is how I can make those sites to know each other if for example, connection established, server accept the connection or not and client know it and send data or trying to connect again. Here is my code in client -Connection() and server - Accept():

int Connect( char *IPaddress ){
	if((sockt = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
		isConnect = -1;
		close(sockt);
		return -1;
	}
	else{
		memset((char *) &servAddr, 0, sizeof(servAddr));
		servAddr.sin_family = AF_INET;
		servAddr.sin_port = htons(PORT);
		inet_aton(IPaddress, &servAddr.sin_addr);
		isConnect = 0;
		byt_read = 0;
		addr_size = sizeof(struct sockaddr);
		
		return 0;
	}
}

int Accept( void ){
	if((sockt = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
		isAccept = -1;
		close(sockt);
		return -1;
	}
	else{
		
			memset((char *) &servAddr, 0, sizeof(servAddr));
			servAddr.sin_family = AF_INET;
			servAddr.sin_port = htons(PORT);
			servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
			if (bind(sockt,(struct sockaddr *)&servAddr, sizeof(servAddr)) == -1){
				isAccept = -1;
				close(sockt);
				return -1;
			}
			else{
					isAccept = 0;
					return 0;
				
			}
		
	}
}

  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2 fkl

fkl

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 417 posts

Posted 13 December 2011 - 10:13 AM

Dude you need some serious conceptual training in socket concepts.

First, there is no connection or accept in a UDP communication. All of these happen only in TCP (Socket type SOCK_STREAM) where as you are using SOCK_DGRAM).

So in udp you just decide on one end to start sending data and on the other end start receiving data. Generally it is the client who initiates a request. However, if you want true parallel 2way communication, in which you don't know which side would start, you need two separate processes or threads one of which waits for incoming data and reads, where as the other writes on the socket.

An alternate is to use select or poll or some similar mechanism which are basically API's which can be called in a loop and would notify you when data is available on a socket.

Lastly, in case of TCP, here is the scheme

you create a socket using socket(),

then you might bind() it to a port / ip. (Not necessary if you are going to be a client sending request since these are automatically filled then),

The you call connect(). This function WOULD HANG and not return until a successful connection is established with a server.

On the other hand in a server, you create socket(), bind it to a port / ip, start listening, and call Accept().

This call wouldn't return until a client actually connects to this listening server. For details you can take a look at the following and other referred tutorials.

http://forum.codecal...inux-c-api.html
  • 0
Today is the first day of the rest of my life

#3 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 13 December 2011 - 10:34 AM

Hello fayyazlodhi ,

Thanks for response. You are right I'm not good on this :( . Basically I have a .c file that contains main method and creates two threads. Client and Server. I have access just on those two files. Those files have three methods each and with a way I have to create a code that when Client request a connection, server answer back if connection established in order to move on or to try again. If connection established client should send three data packets that server should take and answer for each one back to the client, received or not received in order client re-transmit it . At the end when all three packets received from server, client need to send a final ("end") packet that notifying server to close the connection. Server response back that got the data, close the socket and client when received server's confirmation close its socket.

Is more clear now?
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#4 fkl

fkl

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 417 posts

Posted 13 December 2011 - 10:52 AM

It is rare and weird to try to run both client and server out of the same program.

Why cant you simply have two separate applications running on a single pc one acting as a client and the other as a server?
  • 0
Today is the first day of the rest of my life




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download