I'm beginer in LINUX programming.I have some problem. This is simple server. It is compiling successfuly but when i start the program bind function return -1(error code). I dont know where is the problem. Please help if you can.
#include
#include
#include
#include
#include
#include
#include
struct sockaddr_in myaddr, claddr;
struct servent *sp;
int s;
int l, len;
main(){
char msg[10];
if (( sp=getservbyname ("cipo", "udp"))<0)
{
printf("error");
}
myaddr.sin_family=AF_INET;
myaddr.sin_port=sp->s_port;
myaddr.sin_addr.s_addr=INADDR_ANY;
if((s=socket(AF_INET, SOCK_DGRAM, 0))<0)
{
printf("error socket");
}
if (bind(s, &myaddr, sizeof(myaddr))==-1)
{
printf("error bind");
exit(1);
}
// samo serverot pravi bind
len=sizeof(struct sockaddr_in);
l=recvfrom(s, msg, 10, 0, &claddr, &len);
printf("%s\n", msg);
sendto(s, "OK", 3, 0, &claddr, len);
close(s);
}//main_server
Example code on internet is garbage
Usually beginners post their (barely working) source code for you to cut and paste. Also, Microsoft used their worst people (instead of firing them) to write examples and documentation (deliberately -- if you are writing code you are their competitor). Instead, study the documentation carefully, and try to write error-free code from it. Socket code is not that difficult. Sometimes you can start from garbage code and clean it, but usually you rewrite it from scratch if you need to use it seriously.
Also, entire Unix header files for socket stuff are garbage, this in copied into Microsoft "WinSock" also. It all has stupid "Regents of Berkley -- you cannot touch" copyright notice. Extremely complicated spaghetti and also yields garbage resulting code. Instead, analyze header files and write your own, they will only be about 50 lines of code in one header file instead of thousands of lines in 50 interlocking spaghetti header files.
Before you attack me -- my TCP/IP code is deployed with zero bugs -- how about your spaghetti?