| 
 | 
 
 
 
 
 |  | 
2018/10/26(Fri) 08:42:50|NO.85706 
こんにちは。1対2の通信を想定して作っています。
 一台のサーバーに対して2台のクライアントの関係です。
 サーバーを起動させて二台のクライアントの接続を待機する部分を作っているのですが、
 サーバーのポート1つに対して2つのソケットIDで二台と通信することはできるのですか?
 速度的にも1台につきポート1つよりも、ポートは同じでソケットで管理する方がいいのですか?
 説明が雑くてすみません。
 
 | 
 
 
 
 |  | 
2018/10/26(Fri) 09:06:42|NO.85707 
たしかできなかったと思います。というより、元々のソケットというもの自体が1対1の接続を想定したものなので、ソケットをそのままラップしているらしいhspsockでは、サーバー側が複数のソケットを作って待機する必要があったはずです。
 
 | 
 
 
 |  | 
2018/10/26(Fri) 12:29:21|NO.85712 
> hspsockプラグインでサーバーが同一ポートを共有できるように修正
 3.4からsockwaitで単一ポートで待ち受けてクライアントの数だけソケットを作る
 Socket APIでいうところのbind&acceptっぽいことができるようになってるはずです。
 
 速度はどっちのやり方でも大差ないはず。
 
 | 
 
 
 |  | 
2018/10/26(Fri) 17:41:05|NO.85720 
プログラムの大幅変更になってしまうかも知れませんが、hspsockよりpcbnet2のほうが機能が多いのでオススメです。
 | 
 
 
 |  | 
2018/10/28(Sun) 07:34:52|NO.85742 
皆さん返答ありがとうございます。 
とあるプログラマさんの言っておられたpcbnet2は確認させていただきました。 
確かにプログラムの変更点が多いため次回以降に試させていただきます。
 
1対2の通信用プログラムが大体できました。 
クライアントが1バイトデータをそれぞれ10個送信し、それをサーバーで表示するプログラムです。 
しかし、やたらとタイムラグができます。 
アドバイスよろしくお願いします。
 //サーバー
#include "hspsock.as"
onexit*exit
/*---------Client_Wait---------------------------------------------------------*/
 ipget:title refstr
 
 #define Free_Port 2000 //クライアント受付用ポート
 #define Free_Sock 0  //クライアント受付用ソケット
 
 #define Client_MAX 2  //クライアント台数
 
 cient_port = 1  //クライアント接続用ポート 実際はclient_port+Free_Port なので2000番代
 
 cient_sock = 1  //クライアント接続用ソケット
 
 
*ClientAcess
	
 repeat Client_MAX
	sockmake Free_Sock, Free_Port
	
	mes"port:"+Free_Port+",socket:"+Free_Sock+",waiting..."
 
	repeat
 	 sockwait Free_Sock
 	 if(stat == 0):break
 	 if(stat >= 2):dialog"エラー"
	await:loop
 
	mes"port:"+Free_Port+",socket:"+Free_Sock+",accessed"
	
	sockputc cient_port, Free_Sock
 
	sockclose Free_Sock
	
	sockmake cient_sock, cient_port + Free_Port
	
	mes"port:"+(cient_port+Free_Port)+",socket:"+cient_sock+",waiting..."
 
	repeat
 	 sockwait cient_sock
 	 if(stat == 0):break
 	 if(stat >= 2):dialog"エラー"
	await:loop
	
	mes"port:"+(cient_port+Free_Port)+",socket:"+cient_sock+",accessed"
 
	cient_port++
	cient_sock++
	
 loop
 cls 0
 cient_sock = 1
 sockputc 1, cient_sock
 sockputc 1, cient_sock+1	 
/*------------------------------------------------------------------*/
dim Object_vx, 5
dim Object_vy, 5
repeat
 
 cient_sock = 1
 sockputc 1, cient_sock	// クライアント1へ
repeat 5
 	sockgetc Object_vx(cnt), cient_sock
 	sockgetc Object_vy(cnt), cient_sock
loop
sockputc 1, cient_sock+1	 // クライアント2へ
repeat 5
 	sockgetc Object_vx(cnt+5), cient_sock+1
 	sockgetc Object_vy(cnt+5), cient_sock+1
loop
 
 redraw 0
  color 255,255,255:boxf:color 0,0,0
  pos 0,0
   repeat 10
	mes""+Object_vx(cnt)+":"+Object_vy(cnt)+""
   loop
 redraw 1
await :loop
*exit
 end
//クライアント
#include"hspsock.as"
 
 onexit*exit
 
 ipget:title refstr
 
 randomize
 #define Free_Port 2000
 #define Free_Sock 0
 
 #define Server_IP "169.0.0.1"
 
 cient_port = 0
 
 cient_sock = 0
 
 sockopen Free_Sock, Server_IP, Free_Port
	
 repeat
  sockcheck Free_Sock
  if(stat==0):break
 await:loop
 
 sockgetc cient_port, Free_Sock
 	
 sockclose Free_Sock
	
 cient_port += Free_Port
 sockopen cient_sock, Server_IP ,cient_port
 
 repeat
  sockcheck cient_sock
  if(stat==0):break
 await:loop
 
repeat
 repeat
  sockcheck cient_sock
  if(stat==0):break
 await:loop
 
 repeat 5
#if 1	// 0で-15から15までの乱数、1でキーボード操作
  b=0:a=0
  getkey keyup, 38:if(keyup==1):b=-10
  getkey keydw, 40:if(keydw==1):b= 10
  getkey keyri, 39:if(keyri==1):a= 10
  getkey keyle, 37:if(keyle==1):a=-10
  
#else
 a+=(1-rnd(3))
 b+=(1-rnd(3))
 a=limit(a,-15,15)
 b=limit(b,-15,15)
#endif
  sockputc a, cient_sock
  sockputc b, cient_sock
 redraw 0
 color 255,255,255:boxf:color 0,0,0
 pos 0,0
 mes "x:"+a+""
 mes "y:"+b+""
 redraw 1
 loop
 
 await :loop
 
*exit
	sockclose cient_sock
	end
|  
 |  | 
 
 
 |  | 
2018/10/29(Mon) 21:36:38|NO.85750 
意図わからなかったところは適当に書き換えてますが、待ち受けポート1つでsockwait使って 
socketをクライアントあたり2個じゃなくて1個だとこんな感じです。
 // サーバー
#packopt name "socket-server"
#include "hspsock.as"
onexit *exit
ipget:title refstr
#define Free_Port 2000	//クライアント受付用ポート
server_sock = 0	//クライアント受付用ソケット
#define Client_MAX 4  //クライアント台数
client_sock_offset = 1  //クライアント接続用ソケット
*ClientAcess
repeat Client_MAX
	client_id = cnt
	client_sock = client_sock_offset+client_id
	mes"client:"+client_id+" socket:"+client_sock+",waiting..."
	sockmake server_sock, Free_Port
	mes"port:"+Free_Port+",socket:"+server_sock+",waiting..."
	repeat
		sockwait server_sock, client_sock
		if(stat == 0):break
		if(stat >= 2) {
			dialog"エラー"+stat
			goto *exit
		}
		await
	loop
	mes "client:"+client_id+","+client_sock+",accessed"
	repeat client_id+1
		cid = cnt
		client_sock = client_sock_offset + cid
		mes "server->client"+cid
		sockputc 64, client_sock
		sockputc Client_MAX, client_sock
		sockputc client_id+1, client_sock
	loop
loop
repeat Client_MAX
	client_id = cnt
	client_sock = client_sock_offset + client_id
	mes "init:" + clien_id
	sockputc 65, client_sock
	sockputc client_id, client_sock
loop
cls 0
/*------------------------------------------------------------------*/
dim Object_vx, 5 * Client_MAX
dim Object_vy, 5 * Client_MAX
repeat
	repeat Client_MAX
		client_id = cnt
		client_sock = client_sock_offset + client_id
		sockputc 66, client_sock
	loop
	repeat 5
		n = cnt
		repeat Client_MAX
			client_id = cnt
			client_sock = client_sock_offset + client_id
			i = client_id * 5 + n
			sockgetc Object_vx(i), client_sock
			if (stat != 0) : Object_vx(i)=9999
			sockgetc Object_vy(i), client_sock
			if (stat != 0) : Object_vy(i)=9999
		loop
	loop
	redraw 0
	color 255,255,255:boxf:color 0,0,0
	pos 0,0
	repeat Client_Max*5
		mes""+Object_vx(cnt)+":"+Object_vy(cnt)+""
	loop
	redraw 1
	await
loop
*exit
	end
//クライアント
#packopt name "socket-client"
#include"hspsock.as"
onexit *exit
ipget:title refstr
randomize
#define Free_Port 2000
#define Server_IP "127.0.0.1"
cient_sock = 0
sockopen client_sock, Server_IP, Free_Port
if(stat != 0) {
	dialog"エラー"+stat
	goto *exit
}
repeat
	repeat
		sockcheck client_sock
		if(stat==0):break
		redraw 0
		color 255,255,255:boxf:color 0,0,0
		pos 0,0:mes "waiting("+cur+"/"+max+")"
		redraw 1
		await
	loop
	sockgetc v, cient_sock
	if (stat != 0) {
		dialog"エラー"+stat+"code:"+v
		goto *exit
	}
	if (v == 64) {
		sockgetc max, cient_sock
		sockgetc cur, cient_sock
	}
	if (v == 65) {
		sockgetc client_id, cient_sock
		break;
	}
	await
loop
mes "loop my_id:"+client_id
repeat
	sockcheck cient_sock
	if(stat==0):break
	wait
loop
repeat
	repeat
		sockcheck cient_sock
		if(stat==0):break
		await
	loop
	sockgetc v, cient_sock
	if (stat != 0 || v != 66) {
		dialog"エラー"+stat+"code:"+v
		goto *exit
	}
	repeat 5
#if 0	// 0で-15から15までの乱数、1でキーボード操作
		b=0:a=0
		getkey keyup, 38:if(keyup==1):b=-10
		getkey keydw, 40:if(keydw==1):b= 10
		getkey keyri, 39:if(keyri==1):a= 10
		getkey keyle, 37:if(keyle==1):a=-10
#else
		a+=(1-rnd(3))
		b+=(1-rnd(3))
		a=limit(a,-15,15)
		b=limit(b,-15,15)
#endif
		sockputc a, cient_sock
		sockputc b, cient_sock
		redraw 0
		color 255,255,255:boxf:color 0,0,0
		pos 0,0
		mes "id:" + client_id+" cnt"+cnt
		mes "x:"+a+""
		mes "y:"+b+""
		redraw 1
		await 100
	loop
loop
*exit
	sockclose cient_sock
	end
|  
 |  | 
 
 
 |  | 
2018/11/5(Mon) 07:45:26|NO.85788 
返信遅れました。zakkiさんありがとうございました。効率的なプログラムですね。参考にさせていただきます。
 皆さんありがとうございました。
 
 | 
 
 
 |