NetWin problem

General FreeBASIC programming questions.
Post Reply
bojan.dosen
Posts: 166
Joined: May 14, 2007 12:20
Location: Zagreb, Croatia

NetWin problem

Post by bojan.dosen »

Hi,
I have been working with net library (http://www.freebasic.net/forum/viewtopic.php?t=3367). When i compile Server-Client example it works, but only local. When I try start client from different computer, with different ip-address. It does not work! And yes, I am changed this:

Code: Select all

.sin_addr   = build_addr(127,0,0,1) 'localhost
with my ip address, and it works when is run from one computer, but from two computers doesn't!!
What's the problem?
Thanks!
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Post by Gonzo »

can you explain the "error" ?
if you are connecting to a port that isnt open, naturally it will either be connection refused, or connection attempt timed out if there is no refusal
bojan.dosen
Posts: 166
Joined: May 14, 2007 12:20
Location: Zagreb, Croatia

Post by bojan.dosen »

The error is when "NetConnect" is called in client side! Program waits few seconds to connect, but after that variable ret is < 0 so it writes "client error: can't connect to server!". I don't know why. It is only happening when program try to communicate over the internet, but if I start client and server application on same computer it work's normally! How to make this work when client application is on the one computer, and server application on the other with different IP!
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Post by Gonzo »

im not trying to be smug, or too lazy to help
but port forwarding isnt always that simple, and sometimes its not possible if you dont have the access

http://portforward.com/

that site should explain, and help you solve your issue
w_samsa
Posts: 19
Joined: Oct 12, 2011 9:44

Post by w_samsa »

I had your same problem and I solved by changing the port number to 9999

Walter
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Post by Gonzo »

w_samsa wrote:I had your same problem and I solved by changing the port number to 9999

Walter
all ports under 1024 are "special" with a few well known exceptions
such as 21, 22, 23, 80, 81, etc.
they could be blocked even by your ISP, in worst case scenario
best case blocked by your modem, or your own computer

if you decide to use a specific port for your own program, choose something in the 5000-65000 range =)
bojan.dosen
Posts: 166
Joined: May 14, 2007 12:20
Location: Zagreb, Croatia

Post by bojan.dosen »

Thank you everyone! My firewall blocked server application. I don't know why because when Allow/Deny message was shown up I clicked allow, but in firewall settings it was blocked!
Thanks!

I have the other problem now...
When server sends two strings to client:

Code: Select all

NetSendString(hClient, "Hello, ")
NetSendString(hCurrent, "World!")
Client receives one string "Hello, World!" and the other string is empty! I solved this problem by using Sleep 1 between this two commands, but when program is sending a large amount of data on a slower internet connection (especially when using loops) server recognizes two or more strings as one string! Then I increase sleep time to 10 or 20, but then sending time is slowed down 10 or 20 times! My question is how to send a bunch of strings faster that server will recognize as separated strings?
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Post by Gonzo »

you didnt solve anything, you just masked a very important fact about sending and receiving data =)

when you send packets with very short delay between them, they are usually packed together
you ARE NOT SENDING STRINGS
you are sending data

if you want to send "lines" to another destination, you need to have a LINE BREAKER, just like everywhere else

this text im writing has new lines everywhere that makes this text easier on the eyes




like this, because there is a specific character, or characters (CR and LF) that actually have the specific purpose of meaning line-break =)
carriage return (CR) and line feed (LF)

in networking you can choose your own character, or characters that define the end of a message, and a beginning of a new one

here is a short example:

Code: Select all


'' Send data

Const _CR_ = Chr(13)
Const _LF_ = Chr(10)
Const _CRLF_ = WChr(13, 10)

Sub inetSend(d As String)
	
	d += _CRLF_
	
	socket.write(d, Len(d))
	
End Sub

inetSendRaw("Hello, ")

inetSendRaw("world!")

'' And on the other side where you receive:

Dim Shared As String buffer

Sub ReceiveData(textReceived As String)

Dim As String onecharacter
Dim As Integer i

	For i = 1 to len(textReceived)
		
		onecharacter = Mid(textReceived, i, 1)
		
		If onecharacter = _CR_ Or onecharacter = _LF_ Then
			
			If Len(buffer) Then
				
				Print buffer  'this is the received LINE
				
				buffer = ""   'clear buffer, and reuse it
				
			EndIf
			
		Else
			
			buffer += onecharacter
		
		EndIf
	
	Next
	
End Sub
maddogg6
Posts: 824
Joined: Dec 07, 2005 22:58
Contact:

Post by maddogg6 »

Just out of curiosity...

I just learned about serialization (I think I learned correctly) and was thinking (say in case of sending a huge text file, uncompressed) if serializing would help ensure messages get reconstructed correctly? if so, is it used here?, and if not, why?

Would building in serialization into the netwin lib make using it much easier? or just restrict its usefulness?
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Post by Gonzo »

to be honest, the need to serialize (and thus also unserialize) packets, is probably going to be better with your own code, in your own way

its a valuable learning experience though...

in all things programming if you can help it, please make your life easier by just making it as simple as possible :)

the readln and writeln equivalents are quite easy to make, and is all text-based... which gets you through the day =)
but custom stuff most likely isnt as easy for anyone who starts out trying to make a simple binary protocol

buffering is still important to make sure you get whole messages, so if you start each packet with 2 bytes of length data that will let you read it all out
if the next 2 bytes is the command / token id, you can use a memcpy to copy the remaining packet into a struct for that specific packet type

i dont particularly like that type of coding, which leads to alot of structs, specifically one for each type of packet...
there are ways around this in c++, using unions with structs =)

but, someone else will be able to answer that more definitely
i have yet to write a binary socket protocol in FB, but im about to do it before winter =)


and... finally, i hope we are talking about the same thing
im such an airhead, that you never really know...
maddogg6
Posts: 824
Joined: Dec 07, 2005 22:58
Contact:

Post by maddogg6 »

Gonzo wrote:and... finally, i hope we are talking about the same thing
im such an airhead, that you never really know...
no no, it made sense, thanks.
Post Reply