�\�P�b�g�v���O���~���O

�@

1. ��`

�\�P�b�g�F Socket�BTCP/IP �A�v���P�[�V�������쐬���邽�߂̒��ۉ����ꂽ�C���^�[�t�F�[�X�B

�@

2. �\�P�b�g�̎g�p��

�C���^�[�l�b�g�d�b��C���^�[�l�b�g�������܂ނ��ׂĂ� TCP/IP �A�v���P�[�V�����B

�@

3. �\�P�b�g�֐�

�܂��� sockaddr_in �\���̂ɓK�؂ȃp�����[�^�ݒ���s���A�ȉ��̃\�P�b�g�֐������s����B

����
socket �\�P�b�g�̍쐬
bind �\�P�b�g�ƃ|�[�g�̌���
setsockopt �\�P�b�g�I�v�V�����̐ݒ�
close (Linux), closesocket (Win) �\�P�b�g�̏I��
htonl �z�X�g�E�o�C�g�I�[�_�[���l�b�g���[�N�E�o�C�g�I�[�_�[�ɕϊ� (4�o�C�g)
htons �z�X�g�E�o�C�g�I�[�_�[���l�b�g���[�N�E�o�C�g�I�[�_�[�ɕϊ� (2�o�C�g)
ntohl �l�b�g���[�N�E�o�C�g�I�[�_�[���z�X�g�E�o�C�g�I�[�_�[�ɕϊ� (4�o�C�g)
ntohs �l�b�g���[�N�E�o�C�g�I�[�_�[���z�X�g�E�o�C�g�I�[�_�[�ɕϊ� (2�o�C�g)
inet_addr �������IP�A�h���X�ɕϊ�
inet_ntoa IP�A�h���X�𕶎���ɕϊ�
TCP
listen �ڑ��L���[�̍쐬 (�T�[�o��)
accept �ڑ������ (�T�[�o��)
connect �T�[�o�ւ̐ڑ� (�N���C�A���g��)
send �p�P�b�g���M
recv �p�P�b�g��M
UDP
sendto�@ �p�P�b�g���M
recvfrom �p�P�b�g��M

�@

4. �ȒP��TCP/IP�v���O����

�ȉ��́A�N���C�A���g���T�[�o�ɐڑ����A1�b���ƂɃ��b�Z�[�W�𑗐M����v���O�����ł��� (�������A�G���[�������܂������s���Ă��Ȃ������v���O�����ł���)�B�܂��T�[�o�����N�����A�ʂ̃}�V���ŃN���C�A���g�����N������BWindows �p�� Linux �p�̃v���O�����̈Ⴂ�͂قƂ�ǂȂ��A

�C���N���[�h����w�b�_�t�@�C��
Windows �� TCP/IP ��L���ɂ��邽�߂� WSA �֐�
�\�P�b�g�I���̂��߂� closesocket (Windows) �� close (Linux) �̈Ⴂ

�����Ȃ��B����������΁A�\�P�b�g�� OS �Ɉˑ����Ȃ��֐��Q�ł���B

�@

Windows�ŁF �����N�I�v�V������ ws2_32.lib ��������B

TCP �T�[�o��

#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#define BUFFER_SIZE 256

int
main() {
  /* �|�[�g�ԍ��A�\�P�b�g */
  unsigned short port = 9876;
  int srcSocket;  // ����
  int dstSocket;  // ����

  /* sockaddr_in �\���� */
  struct sockaddr_in srcAddr;
  struct sockaddr_in dstAddr;
  int dstAddrSize = sizeof(dstAddr);

  /* �e��p�����[�^ */
  int numrcv;
  char buffer[BUFFER_SIZE];

  /************************************************************/
  /* Windows �Ǝ��̐ݒ� */
  WSADATA data;
  WSAStartup(MAKEWORD(2,0), &data); 

  /* sockaddr_in �\���̂̃Z�b�g */
  memset(&srcAddr, 0, sizeof(srcAddr));
  srcAddr.sin_port = htons(port);
  srcAddr.sin_family = AF_INET;
  srcAddr.sin_addr.s_addr = htonl(INADDR_ANY);

  /* �\�P�b�g�̐��� */
  srcSocket = socket(AF_INET, SOCK_STREAM, 0);

  /* �\�P�b�g�̃o�C���h */
  bind(srcSocket, (struct sockaddr *) &srcAddr, sizeof(srcAddr));

  /* �ڑ��̋��� */
  listen(srcSocket, 1);

  /* �ڑ��̎�t�� */
  printf("Waiting for connection ...\n");
  dstSocket = accept(srcSocket, (struct sockaddr *) &dstAddr, &dstAddrSize);
  printf("Connected from %s\n", inet_ntoa(dstAddr.sin_addr));

  /* �p�P�b�g��M */
  while(1) {
    numrcv = recv(dstSocket, buffer, BUFFER_SIZE, 0); 
    if(numrcv == 0 || numrcv == -1) {
      status = closesocket(dstSocket); break;
    }
    printf("received: %s\n", buffer);
  }

  /* Windows �Ǝ��̐ݒ� */
  WSACleanup();
}
�@

TCP �N���C�A���g��

#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>

int
main() {
  /* IP �A�h���X�A�|�[�g�ԍ��A�\�P�b�g */
  char destination[80];
  unsigned short port = 9876;
  int dstSocket;

  /* sockaddr_in �\���� */
  struct sockaddr_in dstAddr;

  /* �e��p�����[�^ */
  int status;
  int numsnt;
  char *toSendText = "This is a test";

  /************************************************************/

  /* Windows �Ǝ��̐ݒ� */
  WSADATA data;
  WSAStartup(MAKEWORD(2,0), &data));

  /* �����A�h���X�̓��� */
  printf("Connect to ? : (name or IP address) ");
  scanf("%s", destination);

  /* sockaddr_in �\���̂̃Z�b�g */
  memset(&dstAddr, 0, sizeof(dstAddr));
  dstAddr.sin_port = htons(port);
  dstAddr.sin_family = AF_INET;
  stAddr.sin_addr.s_addr = inet_addr(destination);
 
  /* �\�P�b�g���� */
  dstSocket = socket(AF_INET, SOCK_STREAM, 0);

  /* �ڑ� */
  printf("Trying to connect to %s: \n", destination);
  connect(dstSocket, (struct sockaddr *) &dstAddr, sizeof(dstAddr));

  /* �p�P�b�g���o */
  for(i=0; i<10; i++) {
    printf("sending...\n");
    send(dstSocket, toSendText, strlen(toSendText)+1, 0);
    Sleep(1000);
  }

  /* Windows �Ǝ��̐ݒ� */
  closesocket(dstSocket);
  WSACleanup();
}

UDP �T�[�o��

#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#define BUFFER_SIZE 256

int
main() {
  /* �|�[�g�ԍ��A�\�P�b�g */
  int port = 9876;
  int recvSocket;

  /* sockaddr_in �\���� */
  struct sockaddr_in recvSockAddr;

  /* �e��p�����[�^ */
  int status;
  int numrcv;
  char buffer[BUFFER_SIZE];
  unsigned long on = 1;

  /************************************************************/
  /* Windows �Ǝ��̐ݒ� */
  WSADATA data;
  WSAStartup(MAKEWORD(2,0), &data));

  /* sockaddr_in �\���̂̃Z�b�g */
  memset(&recvSockAddr, 0, sizeof(recvSockAddr));
  recvSockAddr.sin_port = htons(port);
  recvSockAddr.sin_family = AF_INET;
  recvSockAddr.sin_addr.s_addr = htonl(INADDR_ANY);

  /* �\�P�b�g���� */
  recvSocket = socket(AF_INET, SOCK_DGRAM, 0);
 
  /* �o�C���h */
  status = bind(recvSocket, (struct sockaddr_in *) &recvSockAddr, sizeof(recvSockAddr));

  /* �p�P�b�g��M */
  while(1) {
    numrcv = recvfrom(recvSocket, buffer, BUFFER_SIZE, 0, NULL, NULL);
    if(numrcv == -1) { status = closesocket(recvSocket); break; }
    printf("received: %s\n", buffer);
  }

  /* Windows �Ǝ��̐ݒ� */
  WSACleanup();
}

UDP �N���C�A���g��

#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>

int
main() {
  /* IP�A�h���X�A�|�[�g�ԍ��A�\�P�b�g */
  char destination[80];
  unsgined short port = 9876;
  int destSocket;

  /* sockaddr_in �\���� */
  struct sockaddr_in destSockAddr;

  /* �e��p�����[�^ */
  int i;
  char *toSendText = "This is a test";

  /************************************************************/
  /* Windows �Ǝ��̐ݒ� */
  WSADATA data;
  WSAStartup(MAKEWORD(2,0), &data));

  /* �����A�h���X�̓��� */
  printf("Connect to ? : (name or IP address) ");
  scanf("%s", destination);

  /* sockaddr_in �\���̂̃Z�b�g */
  memset(&destSockAddr, 0, sizeof(destSockAddr));
  destSockAddr.sin_addr.s_addr = inet_addr(destination);
  destSockAddr.sin_port = htons(port);
  destSockAddr.sin_family = AF_INET;

  /* �\�P�b�g���� */
  destSocket = socket(AF_INET, SOCK_DGRAM, 0);
 
  /* �p�P�b�g���o */
  for(i=0; i<10; i++) {
    printf("sending...\n");
    sendto(destSocket, toSendText, strlen(toSendText)+1, 0, &destSockAddr, sizeof(destSockAddr));
    Sleep(1000);
  }

  /* Windows �Ǝ��̐ݒ� */
  closesocket(destSocket);
  WSACleanup();
}

�@

Linux�ŁF �R���p�C���� "gcc �\�[�X�� -lsocket" �Ƃ���B

TCP �T�[�o��

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define BUFFER_SIZE 256

int
main() {
  /* �|�[�g�ԍ��A�\�P�b�g */
  unsigned short port = 9876;
  int srcSocket;  // ����
  int dstSocket;  // ����

  /* sockaddr_in �\���� */
  struct sockaddr_in srcAddr;
  struct sockaddr_in dstAddr;
  int dstAddrSize = sizeof(dstAddr);

  /* �e��p�����[�^ */
  int numrcv;
  char buffer[BUFFER_SIZE];

  /************************************************************/
  /* sockaddr_in �\���̂̃Z�b�g */
  memset(&srcAddr, 0, sizeof(srcAddr));
  srcAddr.sin_port = htons(port);
  srcAddr.sin_family = AF_INET;
  srcAddr.sin_addr.s_addr = htonl(INADDR_ANY);

  /* �\�P�b�g�̐��� */
  srcSocket = socket(AF_INET, SOCK_STREAM, 0);

  /* �\�P�b�g�̃o�C���h */
  bind(srcSocket, (struct sockaddr *) &srcAddr, sizeof(srcAddr));

  /* �ڑ��̋��� */
  listen(srcSocket, 1);

  /* �ڑ��̎�t�� */
  printf("Waiting for connection ...\n");
  dstSocket = accept(srcSocket, (struct sockaddr *) &dstAddr, &dstAddrSize);
  printf("Connected from %s\n", inet_ntoa(dstAddr.sin_addr));

  /* �p�P�b�g��M */
  while(1) {
    numrcv = recv(dstSocket, buffer, BUFFER_SIZE, 0); 
    if(numrcv == 0 || numrcv == -1) {
      status = close(dstSocket); break;
    }
    printf("received: %s\n", buffer);
  }
}
�@

TCP �N���C�A���g��

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int
main() {
  /* IP �A�h���X�A�|�[�g�ԍ��A�\�P�b�g */
  char destination[80];
  unsigned short port = 9876;
  int dstSocket;

  /* sockaddr_in �\���� */
  struct sockaddr_in dstAddr;

  /* �e��p�����[�^ */
  int status;
  int numsnt;
  char *toSendText = "This is a test";

  /************************************************************/
  /* �����A�h���X�̓��� */
  printf("Connect to ? : (name or IP address) ");
  scanf("%s", destination);

  /* sockaddr_in �\���̂̃Z�b�g */
  memset(&dstAddr, 0, sizeof(dstAddr));
  dstAddr.sin_port = htons(port);
  dstAddr.sin_family = AF_INET;
  stAddr.sin_addr.s_addr = inet_addr(destination);
 
  /* �\�P�b�g���� */
  dstSocket = socket(AF_INET, SOCK_STREAM, 0);

  /* �ڑ� */
  printf("Trying to connect to %s: \n", destination);
  connect(dstSocket, (struct sockaddr *) &dstAddr, sizeof(dstAddr));

  /* �p�P�b�g���o */
  for(i=0; i<10; i++) {
    printf("sending...\n");
    send(dstSocket, toSendText, strlen(toSendText)+1, 0);
    sleep(1);
  }

  /* �\�P�b�g�I�� */
  close(dstSocket);
}

UDP �T�[�o��

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define BUFFER_SIZE 256

int
main() {
  /* �|�[�g�ԍ��A�\�P�b�g */
  unsigned short port = 9876;
  int recvSocket;

  /* sockaddr_in �\���� */
  struct sockaddr_in recvSockAddr;

  /* �e��p�����[�^ */
  int status;
  int numrcv;
  char buffer[BUFFER_SIZE];
  unsigned long on = 1;

  /************************************************************/
  /* sockaddr_in �\���̂̃Z�b�g */
  memset(&recvSockAddr, 0, sizeof(recvSockAddr));
  recvSockAddr.sin_port = htons(port);
  recvSockAddr.sin_family = AF_INET;
  recvSockAddr.sin_addr.s_addr = htonl(INADDR_ANY);

  /* �\�P�b�g���� */
  recvSocket = socket(AF_INET, SOCK_DGRAM, 0);
 
  /* �o�C���h */
  status = bind(recvSocket, (struct sockaddr_in *) &recvSockAddr, sizeof(recvSockAddr));

  /* �p�P�b�g��M */
  while(1) {
    numrcv = recvfrom(recvSocket, buffer, BUFFER_SIZE, 0, NULL, NULL);
    if(numrcv == -1) { status = close(recvSocket); break; }
    printf("received: %s\n", buffer);
  }
}

UDP �N���C�A���g��

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int
main() {
  /* IP�A�h���X�A�|�[�g�ԍ��A�\�P�b�g */
  char destination[80];
  unsgined short port = 9876;
  int destSocket;

  /* sockaddr_in �\���� */
  struct sockaddr_in destSockAddr;

  /* �e��p�����[�^ */
  int i;
  char *toSendText = "This is a test";

  /************************************************************/
  /* �����A�h���X�̓��� */
  printf("Connect to ? : (name or IP address) ");
  scanf("%s", destination);

  /* sockaddr_in �\���̂̃Z�b�g */
  memset(&destSockAddr, 0, sizeof(destSockAddr));
  destSockAddr.sin_addr.s_addr = inet_addr(destination);
  destSockAddr.sin_port = htons(port);
  destSockAddr.sin_family = AF_INET;

  /* �\�P�b�g���� */
  destSocket = socket(AF_INET, SOCK_DGRAM, 0);
 
  /* �p�P�b�g���o */
  for(i=0; i<10; i++) {
    printf("sending...\n");
    sendto(destSocket, toSendText, strlen(toSendText)+1, 0, &destSockAddr, sizeof(destSockAddr));
    sleep(1);
  }

  /* �\�P�b�g�̏I�� */
  close(destSocket);
}