C言語でネットワークプログラミング

まずはソケットにファイルディスクリプタを代入する。

 

socketはシステムコールです。

socketシステムコール

TCPまたはUDP通信を行う場合に、OSにソケットの作成を依頼します。

#include<sys/types.h>
#include<sys/socket.h>

int socket(int domain, int type, int protocol);

 

 

 

ちなみに

domain AF_INET・・・IPv4

type SOCK_STREAM・・・TCP通信

protocol 今回は0

#include <stdio.h>
#include <sys/types.h>

#include <sys/socket.h>

int main(void)
{

int sock;

sock = socket(AF_INET,SOCK_STREAM,0);

if (sock < 0 ){

fprintf(stderr,"sock get ERROR");
}

printf("%d",sock);
}
~
~