Help needed linux socket programming in c


 
Thread Tools Search this Thread
Top Forums Programming Help needed linux socket programming in c
# 1  
Old 10-27-2008
Help needed linux socket programming in c

Good evening everyone! Smilie

I'm doing a small client / server application for sharing files in C, and I am trying to implement the following:

The client of my application sends to the address 255.255.255.255 a message requesting a particular file.In the network there is only one server, however, there may be multiple clients.
The server responds with an ACK if it has the file specified by the client.Until now,everythings' allright.
The question arises now,as I want the client, after receiving the confirmation (ACK) from the server, making sure that the reply message comes from the server or if the message is of uknown origin.As so,I have the following code in Client:

Code:
 ... 
 msg_tam = recvfrom (socketfd, buffer, BUFFERSIZE, 0, (struct sockaddr *) & serv_addr, & Nbytes); 

     if (msg_tam <0) 
      Abort ("..."); 

     buffer [msg_tam] = 0; 

    if (SERV_HOST_PORT == ntohs (serv_addr.sin_port) & & strcmp (SERV_HOST_ADDR, (char *) inet_ntoa (serv_addr.sin_addr) == 0) 
 ( 
 ... 
 ) 
 else 
     printf ( "message of unknown origin!");

The SERV_HOST_ADDR is set to "255.255.255.255" so the client can send the file request to the network and the port is also set with a value.Well, the problem arises when the call to the function recvfrom is made, where the ip address of who sent the message is filled in the structure.That address is ,thus, the address where the server is.
As so, when making the comparison between the SERV_HOST_ADDR in the if statement with the address serv_addr.sin_addr filled in the recvfrom call, they will never be the same (even if the reply has come from the server) ,as a it will always enter the else statement.

I would appreciate if someone could help me solve this problem, so that it would be a way the client could check you check whether the reply comes from the server where the client sent the message or from another place.

Thanks in advance for your help!
# 2  
Old 10-28-2008
The last IP address in a network range is always considered unusable. It is, in fact, used for broadcast messages, meaning that (to my knowledge), you cannot create sockets with it. Also in the code above you have a space between the two ampersands. (& &). If that isn't just an artifact of copying the code into a webform, it's a bug.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

help implementing an ip filter in linux/net/socket.c (kernel programming)

hi there so, i was given an assignment: implement a linux system call that blocks all packets that are sent to a given IP (i have to do it without using iptables) i'm really new to kernel programming btw (and i'm enjoying it a lot) so, my syscall is called ip_block() and receives a... (1 Reply)
Discussion started by: lgfaria
1 Replies

2. Programming

socket programming

how to include socket.h in visual studio 2005.. (2 Replies)
Discussion started by: asd123
2 Replies

3. Programming

Socket programming

Hi everyone, I'm new to this forum. I'm working on new project for last few days and this forum already helped me on couple of occasions. I don't have any prior experience with network programming so I'll appreciate any advise given. I'm trying to do the following: 1. open user... (2 Replies)
Discussion started by: _thomas
2 Replies

4. UNIX for Dummies Questions & Answers

hi i need help with socket programming

in socket programming how can i : Create for example 3 blank files, namely: server, client, network •Server: act as servers/provider, will receive all requests from different client •Client: requesters •Network: middle-layer of communication between server & client any tips or... (6 Replies)
Discussion started by: kedah160
6 Replies

5. Programming

Help with socket programming in C

hi guys i got this code trying to make connection between the server and multi clients but when i do ./server i got message server waiting then when i run ./client it says client 1 nosuch file i dont know whats that should i use any argument plz help how to compile and run and whats the expected... (1 Reply)
Discussion started by: kedah160
1 Replies

6. UNIX for Dummies Questions & Answers

Socket programming

in c socket programming, how can I send and received on the same port? (7 Replies)
Discussion started by: Peevish
7 Replies

7. Programming

Socket programming in C

Hi, i know how to use socket for TCP applications, i also know how to use RAW socket, but i would like to use socket just over IP and bellow TCP, do you know how to do that ? i don't want to reimplement IP :) thanks for your answer. (2 Replies)
Discussion started by: nameless`
2 Replies

8. UNIX for Advanced & Expert Users

socket programming

Hi, I am trying to connect to more than 60 servers to perform some actions remotely. for this I am using ssh2. But its taking lot of time. Though i am running the command in background, still its taking long time to execute.. Any one let me know can we use sockets instead of ssh2 for... (3 Replies)
Discussion started by: pvamsikr
3 Replies

9. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies

10. Programming

Socket programming

Suppose i am writing a C program which is going to use Socket calls. I want to use a Unix port for my Socket. How can i determine a port which is not already in use? (1 Reply)
Discussion started by: Nadeem Mistry
1 Replies
Login or Register to Ask a Question