Internet problem, can't locate it
Blitz3D Forums/Blitz3D Beginners Area/Internet problem, can't locate it
| ||
Made this game in order to learn internet programming, it's a simple twoplayer game where the goal is to place 5 marks in a straight line and prevent your opponent from doing so. The game works fine when I run two copies of it on my own machine and use the local ip for both of them (127.0.0.1). But when playing over the internet, the host can't send his first data package, however the host will recieve the ready message from the client, so some of it works. I'm using "port" 8765 and 8766, whatever that might be, and if that is wrong, then how can the first message go troug? Can anybody help me? If you want to exec it pleace load the game from my storeroom (would like to call it a homepage) Global plno=1 Global x Global y Global turn Const boardsize=16 ip$=Input("Enter opponents IP: ") strmGame=OpenTCPStream(ip,8765) If strmgame=0 Then svrGame=CreateTCPServer(8765) Print "Reciever didn't answer, wait for call" Else WriteString strmGame,"Ready" CloseTCPStream strmgame Print "Reciever answered, establishing contact" plno=2 End If If plno=1 Then Repeat strtream=AcceptTCPStream(svrGame) If strtream<>0 Then alpha$=ReadString$(strtream) If alpha="Ready" Then Exit End If Delay 10 Forever Else svrGame=CreateTCPServer(8766) End If Graphics boardsize*16,boardsize*16,32,2 ClsColor 255,255,0 Cls Color 0,0,0 For a=0 To boardsize Rect a*16-1,0,2,boardsize*16 Rect 0,a*16-1,boardsize*16,2 Next Dim board(boardsize+1,boardsize+1) Dim mark(1) mark(0)=LoadImage("1.bmp") mark(1)=LoadImage("2.bmp") turn=1 Repeat If turn=plno Then Delay 200 .nogood While Not MouseDown(1) Delay 5 Wend x=MouseX()/16 y=MouseY()/16 If board(x+1,y+1)<>0 Then Goto nogood DrawImage mark(plno-1),x*16,y*16 board(x+1,y+1)=plno .nogood2 strmGame=OpenTCPStream(ip,8767-plno) If strmgame=0 Then Goto nogood2 WriteByte strmGame,x WriteByte strmgame,y CloseTCPStream strmgame Else strtream=0 While strtream=0 strtream=AcceptTCPStream(svrGame) Delay 5 Wend x=ReadByte(strtream) y=ReadByte(strtream) board(x+1,y+1)=turn DrawImage mark(2-plno),x*16,y*16 End If Dim l(3) Dim active(7) For nostep=1 To 4 check(0,1,0,nostep) check(1,1,1,nostep) check(1,0,2,nostep) check(1,-1,3,nostep) Next turn=3-turn Forever Function check(x1,y1,lineno,factor) If active(lineno)=0 Then If board(x+1+x1*factor,y+1+y1*factor)=turn Then l(lineno)=l(lineno)+1 If board(x+1+x1*factor,y+1+y1*factor)<>turn Then active(lineno)=1 End If If active(lineno+4)=0 Then If board(x+1-x1*factor,y+1-y1*factor)=turn Then l(lineno)=l(lineno)+1 If board(x+1-x1*factor,y+1-y1*factor)<>turn Then active(lineno+4)=1 End If If l(lineno)=>4 Then Rect 0,0,boardsize*16,16 Color 0,255,255 If turn=plno Then Print "You are victorious!" If turn<>plno Then Print "You have lost!" WaitKey() End End If End Function |
| ||
Hi eBusiness, I can't try your code now, but I have some advice for you: - you appear to close the tcpstream strmgame - if you close it, and then create a new one, the old connection is lost. Do not close it if you want stay connected. - don't use goto - use structured language, that is, functions and the like. - re-design your code completely. Use a 'status' variable, and a select case statement, to decide what to do at each status value. - read my tcp internet chat example, it is in code archive, the link should be in my sig. Hope this helps, Sergio. |
| ||
ReadAvail, didn't notice that command until I saw it in your code, thx. Still dunno what's wrong about my code, but I think I can solve the game now. Think Blitz need a general instruction on how to use the network commands. |
| ||
So, the first game on the internet have been succesfully played, seems like those of my friends that I have tested the game with are not capable of obtaining the server function, and as both players are servers in the above code, it will result in an error, dunno why they can't. Here is the new code, semars code helped me a lot. Global plno=1 Global x Global y Global turn Const boardsize=16 giveaway=0 Print "Enter an adress to call, or press enter in order" ip$=Input("to wait for a call: ") If ip="" Then server=CreateTCPServer(8765) Print "Waiting for call" While conn=0 conn=AcceptTCPStream(server) Delay 100 Wend SeedRnd(MilliSecs()) giveaway=Rand(0,1) Else conn=OpenTCPStream(ip,8765) plno=2 If conn=0 Then End End If Graphics boardsize*16,boardsize*16+16,32,2 ClsColor 255,255,0 Cls Color 0,0,0 For a=0 To boardsize Rect a*16-1,0,2,boardsize*16 Rect 0,a*16-1,boardsize*16,2 Next Dim board(boardsize+1,boardsize+1) Dim mark(1) mark(0)=LoadImage("1.bmp") mark(1)=LoadImage("2.bmp") turn=1+giveaway If giveaway=1 Then WriteShort conn,65535 Repeat Color 0,0,0 Rect 0,256,boardsize*16,16 Color 256,255,255 If turn=plno Then Text 0,256,"Your turn" Delay 200 .nogood While Not MouseDown(1) Delay 5 Wend x=MouseX()/16 y=MouseY()/16 If board(x+1,y+1)<>0 Then Goto nogood DrawImage mark(plno-1),x*16,y*16 board(x+1,y+1)=plno WriteByte conn,x WriteByte conn,y Else Text 0,256,"Your opponent is drawing" While ReadAvail(conn)<2 Delay 5 Wend x=ReadByte(conn) y=ReadByte(conn) If x<>255 Then board(x+1,y+1)=turn DrawImage mark(2-plno),x*16,y*16 End If End If Dim l(3) Dim active(7) For nostep=1 To 4 check(0,1,0,nostep) check(1,1,1,nostep) check(1,0,2,nostep) check(1,-1,3,nostep) Next turn=3-turn Forever Function check(x1,y1,lineno,factor) If active(lineno)=0 Then If board(x+1+x1*factor,y+1+y1*factor)=turn Then l(lineno)=l(lineno)+1 If board(x+1+x1*factor,y+1+y1*factor)<>turn Then active(lineno)=1 End If If active(lineno+4)=0 Then If board(x+1-x1*factor,y+1-y1*factor)=turn Then l(lineno)=l(lineno)+1 If board(x+1-x1*factor,y+1-y1*factor)<>turn Then active(lineno+4)=1 End If If l(lineno)=>4 Then Color 0,0,0 Rect 0,256,boardsize*16,16 Color 256,255,255 If turn=plno Then Text 0,256,"You are victorious!" If turn<>plno Then Text 0,256,"You have lost!" WaitKey() End End If End Function Think it need some minor gameplay changes thou. Edit: Minor gamplay changes have been added. |