1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ソケットプログラミング LEVEL1

Last updated at Posted at 2015-05-29

ソケットプログラミング

clがsvに接続すると、clからsvに対して、データとpidを送る。
svは、clからデータを受信すると標準出力に受信データを表示してclとのコネクションを切断し、新しいコネクション待ちになる。

ディレクトリ構造は以下

top
├── Makefile
├── bin
│   ├── cl
│   └── sv
├── obj
│   ├── cl.o
│   └── sv.o
└── src
    ├── cl.c
    └── sv.c

makefileのソースコード

BIN_DIR=bin
SRC_DIR=src
OBJ_DIR=obj

TARGET_CL=$(BIN_DIR)/cl
TARGET_SV=$(BIN_DIR)/sv

vpath %.c $(SRC_DIR)

all:$(TARGET_CL) $(TARGET_SV)
$(TARGET_CL):$(OBJ_DIR)/cl.o
	$(CC) -o $@ $^
$(TARGET_SV):$(OBJ_DIR)/sv.o
	$(CC) -o $@ $^
$(OBJ_DIR)/%.o:%.c
	$(CC) -Wall -g -c $< -o $@
clean:
	@rm -f obj/* bin/*

サーバ側のソース

sv.c
# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# include<unistd.h>
# include<sys/socket.h>
# include<sys/types.h>
# include<netinet/in.h>
# define SV_PORT (11111)
int main(){
    int sock;
    int sock_accept;
    unsigned int len;
    char buf[256]={0};
    int ret;
    struct sockaddr_in addr;
    struct sockaddr_in client;
    if((sock=socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("socket");
        exit(1);
    }
    addr.sin_family = AF_INET;
    addr.sin_port = htons(SV_PORT);
    addr.sin_addr.s_addr=INADDR_ANY;
    ret=bind(sock, (struct sockaddr *)&addr, sizeof(addr));
    if(ret==-1)
    {
        perror("bind");
        exit(1);
    }
    listen(sock, 5);
    while(1)
    {
        len=sizeof(client);
        sock_accept=accept(sock, (struct sockaddr *)&client, &len);
        if(sock_accept == -1)
        {
             perror("accpet");
             close(sock);
             exit(1);
        }
        recv(sock_accept, buf, sizeof(buf), 0);
        printf("recv:%s\n",buf);
        close(sock_accept);
    }
    //close(sock);
    
    return 0;
}


クライアント側のソース

cl.c
# include<stdio.h>
# include<string.h>
# include<unistd.h>
# include<arpa/inet.h>
# include<sys/socket.h>
# include<sys/types.h>
# include<netinet/in.h>
# define SV_PORT 11111
# define SV_IP "127.0.0.1"
int main(){
  int sock;
  char data[32]="cl_data_OK";
  char buf[256]={0};
  struct sockaddr_in dest;
  if((sock=socket(AF_INET, SOCK_STREAM, 0)) < 0)
  {
    perror("socket");
    return -1;
  }
  dest.sin_family = AF_INET;
  dest.sin_port = htons(SV_PORT);
  dest.sin_addr.s_addr=inet_addr(SV_IP);

  if(connect(sock, (struct sockaddr *)&dest, sizeof(dest)) == -1)
  {
    perror("connect");
  }

  sprintf(buf,"data=%s length=%lu pid=%d\n",data, strlen(data), getpid());
  send(sock, buf, strlen(buf), 0);
  printf("send:%s\n",buf);

  close(sock);
  
  return 0;
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?