使用言語はVC++2010、DXライブラリです
UDP通信でなんとか4人までの座標の位置を表示させることはできたんですが、
接続するのに使用するポートを全員開放してないと通信プレイできません。
他のネット対戦できるゲームなどを見るとホスト(サーバー)だけポート開放すれば
ネット対戦できますよね?
どうしたら出来るんでしょうか?
それともUDPじゃ出来ないんでしょうか?
それともし好ければ通信プレイのよく使われるソースの書き方や仕組みなど教えてくださいw
一応ソースを載せておきます
アドバイスなどあればお願いします!
void NetSocketSend(){
// UDP通信用のソケットハンドルを作成
NetUDPHandle = MakeUDPSocket( 10800 ) ;
// IPアドレスを設定( ここにある4つのIP値は仮です )
Ip.d1 = 000 ;
Ip.d2 = 000 ;
Ip.d3 = 000 ;
Ip.d4 = 000 ;
}
void setuzokuOK(){
char setuzoku[/url]="000";
NetWorkSendUDP( NetUDPHandle, Ip, 10800, setuzoku , 4 ) ;
// 文字列の受信を待つ
while( CheckNetWorkRecvUDP( NetUDPHandle ) == FALSE )
{
// ウインドウズメッセージ処理
if( ProcessMessage() < 0 ){
mode=999 ;
break;
}
}
NetWorkRecvUDP( NetUDPHandle, NULL, NULL, StrBuf, 4, NULL ) ;
if(strncmp(StrBuf, "000", 3)==0){
Netmode+=10;
}else mode=999;
}
void NetSocketRecvHost(){
NetUDPHandle = MakeUDPSocket( 10800 ) ;
Netmode+=10;
}
void NetmodeSend(){
char chxp[/url]="9990000";
char chyp[/url]="9990000";
sprintf(chxp,"001%04d",ch.x);
sprintf(chyp,"002%04d",ch.y);
NetWorkSendUDP( NetUDPHandle, Ip, 10800, chxp , 8 ) ;
NetWorkSendUDP( NetUDPHandle, Ip, 10800, chyp , 8 ) ;
}
void NetmodeSendHost(){
char chxp[/url]="9990000";
char chyp[/url]="9990000";
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(Ipflag==1){
if(i==j){
sprintf(chxp,"%d01%04d",i,ch.x);
sprintf(chyp,"%d02%04d",i,ch.y);
}else{
sprintf(chxp,"%d01%04d",i,chNetx);
sprintf(chyp,"%d02%04d",i,chNety);
}
NetWorkSendUDP( NetUDPHandle, IpS[j], 10800, chxp , 8 ) ;
NetWorkSendUDP( NetUDPHandle, IpS[j], 10800, chyp , 8 ) ;
}
}
}
}
void NetmodeRecvHost(){
char StrBuf2[ 256 ] ; // データバッファ
int i,j,k;
while(CheckNetWorkRecvUDP( NetUDPHandle ) == TRUE){
NetWorkRecvUDP( NetUDPHandle, &Ips, NULL, StrBuf, 8, NULL) ;
if(strncmp(StrBuf, "000", 3)==0){
for(i=0;i<3;i++){
if(Ipflag==0){
NetWorkSendUDP( NetUDPHandle, Ips, 10800, "000" , 4 ) ;
Ipflag=1;
IpS.d1 = Ips.d1 ;
IpS.d2 = Ips.d2 ;
IpS.d3 = Ips.d3 ;
IpS.d4 = Ips.d4 ;
i=3;
}
}
}
for(i=0;i<3;i++){
if(Ipflag==1){
if(IpS[i].d1 == Ips.d1&&IpS[i].d2 == Ips.d2&&IpS[i].d3 == Ips.d3&&IpS[i].d4 == Ips.d4){
if(strncmp(StrBuf, "001", 3)==0){
k = strlen(StrBuf);
for(j=3;j<k;j++){
StrBuf2[j-3] = StrBuf[j];
}
StrBuf2[j]='\0';
chNetx[i] = atoi(StrBuf2);
}
if(strncmp(StrBuf, "002", 3)==0){
k = strlen(StrBuf);
for(j=3;j<k;j++){
StrBuf2[j-3] = StrBuf[j];
}
StrBuf2[j]='\0';
chNety[i] = atoi(StrBuf2);
}
}
}
}
}
}
void NetmodeRecv(){
char StrBuf2[ 256 ] ; // データバッファ
int i,j;
while(CheckNetWorkRecvUDP( NetUDPHandle ) == TRUE){
NetWorkRecvUDP( NetUDPHandle, NULL, NULL, StrBuf, 8, NULL) ;
if(strncmp(StrBuf, "001", 3)==0){
i = strlen(StrBuf);
for(j=3;j<i;j++){
StrBuf2[j-3] = StrBuf[j];
}
StrBuf2[j]='\0';
chNetx[0] = atoi(StrBuf2);
}
if(strncmp(StrBuf, "002", 3)==0){
i = strlen(StrBuf);
for(j=3;j<i;j++){
StrBuf2[j-3] = StrBuf[j];
}
StrBuf2[j]='\0';
chNety[0] = atoi(StrBuf2);
}
if(strncmp(StrBuf, "101", 3)==0){
i = strlen(StrBuf);
for(j=3;j<i;j++){
StrBuf2[j-3] = StrBuf[j];
}
StrBuf2[j]='\0';
chNetx[1] = atoi(StrBuf2);
}
if(strncmp(StrBuf, "102", 3)==0){
i = strlen(StrBuf);
for(j=3;j<i;j++){
StrBuf2[j-3] = StrBuf[j];
}
StrBuf2[j]='\0';
chNety[1] = atoi(StrBuf2);
}
if(strncmp(StrBuf, "201", 3)==0){
i = strlen(StrBuf);
for(j=3;j<i;j++){
StrBuf2[j-3] = StrBuf[j];
}
StrBuf2[j]='\0';
chNetx[2] = atoi(StrBuf2);
}
if(strncmp(StrBuf, "202", 3)==0){
i = strlen(StrBuf);
for(j=3;j<i;j++){
StrBuf2[j-3] = StrBuf[j];
}
StrBuf2[j]='\0';
chNety[2] = atoi(StrBuf2);
}
}
}
void NetmodeMain(){
if(Netmode==0){
NetSocketSend();
setuzokuOK();
}else if(Netmode==1){
NetSocketRecvHost();
}
if(Netmode==10){
NetmodeSend();
NetmodeRecv();
}else if(Netmode==11){
NetmodeSendHost();
NetmodeRecvHost();
}
}