C Socket Programming #2 – Creating a Server
Last time we connected to a server; this time we play the other side. In this post we create a socket and bind it to a specific port so that it can take connections.
We learned how to create a socket in our 1st post, so here we go one step further and claim the port with the bind command.
int socket_desc;
struct sockaddr_in server;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("bind failed");
}
puts("bind done");
This also makes one thing clear: two different sockets cannot bind the same port.
Once we have opened a port, the natural next step is to listen on it, and for that we use the listen function.
listen(socket_desc , 3);
If we add listen to the code above, then compile and run it, simply pointing our browser at localhost:8888 is enough to reach that socket.
The listen function takes 2 parameters: our socket and the number of clients it will listen for at that moment (the backlog). If we enter a number less than 0 here, it will still start it as 0.
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
int main(int argc , char *argv[])
{
int socket_desc , new_socket , c;
struct sockaddr_in server , client;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("bind failed");
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (new_socket<0)
{
perror("accept failed");
}
puts("Connection accepted");
return 0;
}
When a connection actually arrives, we assign the accept function to a new socket.
The accept function creates a new socket using the same protocol type and address family as the socket we have.
It takes 3 parameters: the socket we created, the address of the bound socket, and the address length.
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
source: http://www.binarytides.com/socket-programming-c-linux-tutorial/
– Turkish Version –
C Soket Programlama #2 – Server oluşturuyoruz
Geçen sefer bir servera bağlanmıştık; bu sefer işin diğer tarafındayız. Bu yazımızda bir soket oluşturup onu belirli bir porta bind ediyoruz ki bağlantıları kabul edebilsin.
Soket oluşturmayı 1. yazımızda anlamıştık, o yüzden burada bir adım ileri gidip bind komutuyla portu alıyoruz.
int socket_desc;
struct sockaddr_in server;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("bind failed");
}
puts("bind done");
Bu aynı zamanda bir şeyi netleştiriyor: 2 farklı soket aynı portu bind edemez.
Port açtığımıza göre doğal olarak bir sonraki adım bunu dinlemek, bunun için de listen fonksiyonunu kullanıyoruz.
listen(socket_desc , 3);
Yukarıdaki kodlara listen eklersek, kodları derleyip çalıştırdığımızda tarayıcımıza localhost:8888 adresine gitmesini söylememiz o sokete erişmek için yeterlidir.
listen fonksiyonu 2 tane parametre alıyor: soketimiz ve o anda kaç client listen edeceğinin sayısı (backlog). Eğer buraya 0’dan küçük bir sayı girersek yine 0 olarak başlatacaktır.
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
int main(int argc , char *argv[])
{
int socket_desc , new_socket , c;
struct sockaddr_in server , client;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("bind failed");
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (new_socket<0)
{
perror("accept failed");
}
puts("Connection accepted");
return 0;
}
Bir bağlantı gerçekten geldiğinde yeni bir sokete accept fonksiyonunu atıyoruz.
accept fonksiyonu elimizdeki soketteki aynı tip protokol ve adres ailesini kullanarak yeni soket oluşturuyor.
3 tane parametre alır. Bunlar oluşturduğumuz soket, bind edilen soketin adresi ve adres_len.
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
kaynak : http://www.binarytides.com/socket-programming-c-linux-tutorial/