質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

Q&A

1回答

1630閲覧

サーバとクライアント間でのプログラム

monamona154

総合スコア13

Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

0グッド

0クリップ

投稿2016/07/03 07:31

C言語でサーバとプログラム間でのプログラムを作成したいのです。
サーバを起動してクライアントが1~9の数字をどれかの数字を送信する→サーバが数字を受け取る→その送られた数字の段の九九をクライアントに送信して表示 というのを1つのサーバと2つ以上のクライアントを使用して作成したいです。
Linuxで端末を複数起動してやりたいです。
tcpでチャットが出来るプログラムが以下になります。
これを修正して九九が出るように修正してもらいたいです。
九九を返すためには、どんな作業を行えば良いのでしょうか?
回答お願いします。

C言語

1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4#include <unistd.h> 5#include <netdb.h> 6#include <sys/socket.h> 7#include <arpa/inet.h> 8 9#define PORT 20007 10#define BUF_SIZE 0x1000 11 12 static void server(int port){ 13 struct sockaddr_in sin; 14 fd_set rfds; 15 int ld, sd[5], len, max,x,y; 16 char *buf, str[256]; 17 18 if ((buf = malloc(BUF_SIZE)) == NULL) { 19 perror("malloc"); 20 return; 21 } 22 23for(x=0;x<5;x++) sd[x] = -1; 24 25 if ((ld = socket(PF_INET, SOCK_STREAM, 0)) < 0) { 26 perror("socket"); 27 goto close_and_end; 28 } 29 30 memset(&sin, 0, sizeof(sin)); 31 sin.sin_family = AF_INET; 32 sin.sin_port = htons(port); 33 if (bind(ld, (struct sockaddr*)&sin, sizeof(sin)) < 0) { 34 perror("bind"); 35 goto close_and_end; 36 } 37 if (listen(ld, 3) < 0) { 38 perror("listen"); 39 goto close_and_end; 40 } 41 42 loop: 43 FD_ZERO(&rfds); 44 FD_SET(ld, &rfds); 45 max = ld; 46 47for(x=0;x<5;x++){ 48 if (sd[x] >= 0) { 49 FD_SET(sd[x], &rfds); 50 if (max < sd[x]) max = sd[x]; 51 } 52 } 53 54 select(max + 1, &rfds, NULL, NULL, NULL); 55 56for(x=0;x<5;x++){ 57 if (sd[x] > 0 && FD_ISSET(sd[x], &rfds)) { 58 if ((len = read(sd[x], buf, BUF_SIZE)) > 0) { 59 if (write(sd[x], buf, len) < 0) { 60 close(sd[x]); 61 sd[x] = -1; 62 } 63for(y=0;y<5;y++){ 64 if(x!=y){ 65 if (sd[y] > 0) { 66 if (write(sd[y], buf, len) < 0) { 67 close(sd[y]); 68 sd[y] = -1; 69 } 70 } 71 } 72 } 73 } 74 else { 75 if (len < 0) perror("read"); 76 close(sd[x]); 77 sd[x] = -1; 78 } 79 } 80 } 81 82 if (FD_ISSET(ld, &rfds)) { 83 int tmpd; 84 85 len = sizeof(sin); 86 if ((tmpd = accept(ld, (struct sockaddr*)&sin, (socklen_t*)&len)) 87 < 0) { 88 perror("accept"); 89 goto close_and_end; 90 } 91 inet_ntop(AF_INET, &sin.sin_addr.s_addr, str, sizeof (str)); 92 printf("connected from %s\n", str); 93 94 if(sd[0]<0) 95 sd[0]=tmpd; 96 else if(sd[1]<0) 97 sd[1]=tmpd; 98 else if(sd[2]<0) 99 sd[2]=tmpd; 100 else if(sd[3]<0) 101 sd[3]=tmpd; 102 else if(sd[4]<0) 103 sd[4]=tmpd; 104 else if(sd[5]<0) 105 sd[5]=tmpd; 106 else{ 107 strcpy(buf, "Server too busy\n"); 108 send(tmpd, buf, strlen(buf), 0); 109 close(tmpd); 110 } 111 } 112 goto loop; 113 114 close_and_end: 115 for(x=0;x<5;x++){ 116 if (sd[x] >= 0) close(sd[x]); 117 if (ld >= 0) close(ld); 118 } 119 120 free(buf); 121} 122 123 static void client(unsigned int ip, int port){ 124 struct sockaddr_in sin; 125 char *buf; 126 int sd, len, i; 127 fd_set rfds; 128 int max; 129 130 if ((buf = malloc(BUF_SIZE)) == NULL) { 131 perror("malloc"); 132 return; 133 } 134 135 if ((sd = socket(PF_INET, SOCK_STREAM, 0)) < 0) { 136 perror("socket"); 137 goto close_and_end; 138 } 139 memset(&sin, 0, sizeof(sin)); 140 sin.sin_family = AF_INET; 141 sin.sin_port = htons(port); 142 sin.sin_addr.s_addr = htonl(ip); 143 if (connect(sd, (struct sockaddr*)&sin, sizeof(sin)) < 0) { 144 perror("connect"); 145 goto close_and_end; 146 } 147 148loop: 149 FD_ZERO(&rfds); 150 max=1; 151 152 if (0 < sd){ 153 FD_SET(sd, &rfds); 154 if(max<sd) max = sd; 155 } 156 157 FD_SET(0, &rfds); 158 159 select(max + 1, &rfds, NULL, NULL, NULL); 160 161 if(FD_ISSET(0, &rfds)){ 162 if (fgets(buf, BUF_SIZE, stdin) <= 0) 163 goto close_and_end; 164 len = strlen(buf); 165 if (send(sd, buf, len, 0) < 0) { 166 perror("send"); 167 goto close_and_end; 168 } 169 } 170 171 if(FD_ISSET(sd, &rfds)){ 172 len = recv(sd, buf, BUF_SIZE, 0); 173 if (len <= 0) { 174 if (len < 0) perror("send"); 175 goto close_and_end; 176 } 177 for (i = 0; i < len; i++) putchar(buf[i]); 178 } 179 goto loop; 180 181 close_and_end: 182 if (sd >= STDIN_FILENO) close(sd); 183 if (sd >= 0) close(sd); 184 free(buf); 185} 186 187 static void usage(void){ 188 printf("server mode: tcp2 -s\n"); 189 printf("client mode: tcp2 -c\n"); 190 exit(0); 191} 192 193 int main(int argc, char **argv, char **env){ 194 if (argc != 2) usage(); 195 if (argv[1][0] != '-') usage(); 196 switch (argv[1][1]) { 197 case 's': 198 server(PORT); 199 break; 200 case 'c': 201 { 202 struct hostent *ent; 203 204 ent = gethostbyname("localhost"); 205 client(ntohl(*(int*)ent->h_addr_list[0]), PORT); 206 break; 207 } 208 default: 209 usage(); 210 } 211 212 exit(0); 213}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

https://teratail.com/questions/39680
これはどうしたんですか?質問を変えて同じ内容を聞いているように思えます。

投稿2016/07/03 07:38

shi_ue

総合スコア4437

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問