Code:
#include<unistd.h>
#include <WinSock2.h>
int main()
{ int sock,new;
struct sockaddr_in server,client;
int len=sizeof(struct sockaddr);
char msg[]="first tcp sockets programme\n";
if (sock=socket(AF_INET,SOCK_STREAM,0))
{ perror("socket:");
exit(-1);
}
//now bind the server port
server.sin_family=AF_INET;
server.sin_port =htons(9000);
server.sin_addr.s_addr=INADDR_ANY;
bzero(&server.sin_zero, 8);
if (bind(sock,(struct sockaddr *)&server,sizeof(struct sockaddr) ))
{perror("bind error:");
exit(-1);
}
listen(sock,2);
// listen seldom gines error
while(1)
{if(new=accept(sock,(struct sockaddr *)&client,&len))
{perror("accept:");
exit(-1);
}
//sending message
fgets(msg ,20,stdin);
send(new,msg,strlen(msg),0);
}
closesocket(new);//describe the client socket
closesocket(sock);
}
as for the formatting, pick one style and stick with it, it doesnt really matter too much how you format, but mixing styles makes your code look sloppy.
Code:
if(condition)
{ statement();
}
is very unusual, although syntactically correct.