|
-
May 20th, 2013, 10:50 AM
#1
Thread Starter
New Member
Winsock Chat Application HELP
Okay so I'm making a one to one connection chat application using Winsock.
I just had a question, if I sent the client to a friend from another location, in the code for the application what IP would they be connecting to?
Is it MY Ip?
Also if it is MY ip, which would be the correct method of doing this?
.remotehost = "xxx.xxx.xx.x"
OR
.remotehostip = "xxx.xxx.xx.x"
^ When I tried this method it gave me an error saying "Wrong number of arguments or invalid property assigment"
Thank you.
-
May 20th, 2013, 11:28 AM
#2
Re: Winsock Chat Application HELP
RemoteHostIP is read only
The IP you want to use for your friend to connect to you is your external IP
Winsock.Close
Winsock.Connect "nnn.nnn.nnn.nnn", nnnn
"nnn.nnn.nnn.nnn" is the IP to connect to
nnnn is port number
NOTE: Before your friend can connect to you, you must insure that your router allows the port number and that there is no blocking of the connection (firewall, for example).
Last edited by jmsrickland; May 20th, 2013 at 11:48 AM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 20th, 2013, 12:02 PM
#3
Thread Starter
New Member
Re: Winsock Chat Application HELP
Thank you for the quick and great answer. Another question if you don't mind answer,
Is Winsock.senddata only limited to strings, or can this be files or anything else?
-
May 20th, 2013, 12:48 PM
#4
Re: Winsock Chat Application HELP
SendData sends data. Data is what ever you define it to be.
Dim MyString As String
Dim MyArray() As Byte
Dim MyNumber As Integer
Winsock.SendData MyString
Winsock.SendData MyArray
Winsock.SendData MyNumber
Dim MyBinaryData As String
Open path For Binary As #1
Get #1, 1, MyBinaryData
Close #1
Winsock.SendData MyBinaryData
Sending data is one thing, receiving data is another
When you receive data it may come in as one whole piece of data or it may be fragmented into separate chunks. It's up to you how you handle the receiving end. Usually and recommended is that you buffer up the data until all the data has been received. Put some code on the end of the data to tell the receiver that this is the end of the data. I use Chr(30).
MyString = "the data string you want to send" & Chr(30)
Winsock.SendData MyString
At the receiving end:
Dim Buffer As String
Private Sub Winsock_DataArrival(.....)
Dim s As String
Winsock.GetData s, vbString
Buffer = Buffer & s
If Right(Buffer, 1) = Chr(30) Then
'
' process data; it's all been received
'
End If
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 20th, 2013, 01:02 PM
#5
Re: Winsock Chat Application HELP
If you would like a simple example I put one in the Code Bank. It's a combination voice and text chat app.
http://www.vbforums.com/showthread.p...at-Application
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 21st, 2013, 11:06 AM
#6
Thread Starter
New Member
Re: Winsock Chat Application HELP
Sorry about the late reply and thank you. I've got it worked for a one on one connection but now I'm wondering how I would get it working for multiple connections.
Arraying the Winsock I suppose would work but after I got let's 100 users connected, and one of them sends data, so in my data arrival event how would I determine which winsock(index) was assigned to that user?
-
May 21st, 2013, 11:24 AM
#7
Thread Starter
New Member
Re: Winsock Chat Application HELP
Oh, sorry I was a bit confused for a second. I think I have it all mixed up. If I have a array of let's say 100 Winsocks and a user connects to one of them, then when he sends data and it arrives, Winsock(index) will recongize which index of winsock it should be sending data to, am I correct?
-
May 21st, 2013, 11:40 AM
#8
Re: Winsock Chat Application HELP
Yes. That is what Index is for. Keep in mind that each user should have his own buffer (see post #4) for data arrival otherwise you will get different messages coming in from different users all in the same buffer and that would be too much trouble to separate. Also make sure you terminate each message with a termination code (see post #4)
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 21st, 2013, 02:14 PM
#9
Thread Starter
New Member
Re: Winsock Chat Application HELP
Alright thanks for the answer. I'll try it with index and let you know. Just curious, have you made a chat room of your own before?
Last edited by plantinum23; May 21st, 2013 at 03:34 PM.
-
May 21st, 2013, 03:36 PM
#10
Re: Winsock Chat Application HELP
Yes. See post #5 and also I put another multi client chat in the Code Bank here http://www.vbforums.com/showthread.p...at-Application
Last edited by jmsrickland; May 21st, 2013 at 03:41 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 21st, 2013, 03:56 PM
#11
Thread Starter
New Member
Re: Winsock Chat Application HELP
Ah alright, I don't need a new port for every connection coming in right? Let's say there are 100 clients connecting to me, so they're all supposed to connect to the same port correct? For example, winsock.connect "xxx.xxx.xxx.xxx", 5000
-
May 21st, 2013, 04:19 PM
#12
Re: Winsock Chat Application HELP
All users connect to same IP address and same Port number
Your server determines the IP and port number. For your friends to connect to you on your PC your server must use your EXTERNAL IP address. Any port number will do if kept in the 1000 and above. Your friends must know what the IP is and the port number
Client:
Winsock.Connect "xxx.xxx.xxx.xxx", 5000 where xxx.xxx.xxx.xxx is your EXTERNAL IP, not your internal ("localhost"). You use "localhost" when you are testing server and client on same machine.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 21st, 2013, 04:52 PM
#13
Thread Starter
New Member
Re: Winsock Chat Application HELP
Okay thanks, makes sense. I'm pretty sure I would have to port-forward the port, I use to be able to port forward ports before until it suddenly stopped and now I cannot. If you think you could help me, is it fine if you use team viewer and help me set it up? Hope I'm not bothering you by asking this.
EDIT: Got port-forwarding to work.
Last edited by plantinum23; May 21st, 2013 at 05:57 PM.
-
May 21st, 2013, 06:06 PM
#14
Thread Starter
New Member
Re: Winsock Chat Application HELP
Ugh now I'm getting stuck even more.
If I'm using an array of winsock on the server, how would I assign a localport and listen to it for every particular winsock? It gave me an error when I tried it with an for loop.
Also, I'm using localport, is it fine if it's over the internet and not on the same computer? If not, what should I use? <-- On the server
Last edited by plantinum23; May 21st, 2013 at 06:19 PM.
-
May 21st, 2013, 07:17 PM
#15
Re: Winsock Chat Application HELP
You should look at my two examples I have in the Code Bank they will give you much insight.
For the Server you use one Winsock control. Let's call this ServerSocket. This will be an array so initialize it's Index property to 0 at Design Time
Note: You could use two sockets; one for listening and the other for connections but for this example we'll use just one socket
Your Server will listen on ServerSocket(0)
Put below code in your Server's Form_Load event
ServerSocket(0).Close
ServerSocket(0).LocalIP = "xxx.xxx.xxx.xxx" <----- for testing on same machine use "LocalHost"; For Internet use EXTERNAL IP
ServerSocket(0).LocalPort = 5000
ServerSocket(0).Listen
Now your server is listening for incoming connections.
When you get your first connection your Server's ConnectionRequest() event is fired. In this event you load a new socket with index of 1
Load ServerSocket(1)
Then you use this socket to communicate with the first client
ServerSocket(1).Accept requestID
Now your server and the first client can communicate. If the client sends you a message it will arrive in your DataArrival() event and Index will be the value of the socket that sent the message.
When the second connection comes in you will load ServerSocket(2) in your ConnectionRequest() event. For each connection that comes in use the next Index value and load the next ServerSocket. Since you can't hard code these Index numbers you will need to keep a Index counter for loading sockets.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 21st, 2013, 08:58 PM
#16
Banned
Re: Winsock Chat Application HELP
 Originally Posted by plantinum23
Oh, sorry I was a bit confused for a second. I think I have it all mixed up. If I have a array of let's say 100 Winsocks and a user connects to one of them, then when he sends data and it arrives, Winsock(index) will recongize which index of winsock it should be sending data to, am I correct?
just telling you what i do
say 100 users are connected to server now
and i wish to connect to that server including me there will be 101 users connected
when you first connect to host from client
in client you have this code i think james posted
in winsock connect event
winsock1.senddata "|100|" & "tonymontanna"
now tonymontanna is connected to the server with other users
now that 101 users are connected ? how do we send text message from 1 client to another ?
since we using usernames we can use that to send out data to specific user
since a client sends out a data to the server server bounce that msg and sends it out to all
but when it comes to msging 1 person only this is what i do
winsock1.senddata "|txtmsg|" & "tonymontanna"
in client make sure you added this code so when the server sounds out to all clients the client dataarrival will pick this up and identify the msg
in winsock dataarrival
' we need to use the split code here
if instr(data,"|txtmsg|" then
dim ddd as string
split the |txtmsg| and tonymontanna
ddd = tonymontannatonymontanna
if instr(text1.text,ddd) then
show my msg
end if
end if
sorry typing from mobile
-
May 21st, 2013, 09:18 PM
#17
Thread Starter
New Member
Re: Winsock Chat Application HELP
Thanks, that is a good idea will definetly do that.
"ServerSocket(0).LocalIP = "xxx.xxx.xxx.xxx" <----- for testing on same machine use "LocalHost"; For Internet use EXTERNAL IP" <-- It is giving me an error because I believe .LocalIP is read-only
-
May 21st, 2013, 09:53 PM
#18
Re: Winsock Chat Application HELP
You're right. Don't know what I was thinking. Just remove that line.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 21st, 2013, 09:54 PM
#19
Banned
Re: Winsock Chat Application HELP
ServerSocket(0).connect "localhost",3333 ' 3333 is your port number what ever ur using
this will connect client to your server
not sure what post 18 u trying to do sir
-
May 21st, 2013, 10:16 PM
#20
Thread Starter
New Member
Re: Winsock Chat Application HELP
Alright thanks guys, it's working now.
-
May 21st, 2013, 10:51 PM
#21
Thread Starter
New Member
Re: Winsock Chat Application HELP
Uh do you know by any chance why this line is giving me a type mismatch error?
dim loadsocket = 1
under form_load
Load (Winsock(loadsocket))
end sub
Load (Winsock(loadsocket)) <-- This line is giving me a type mismatch error
-
May 22nd, 2013, 01:01 AM
#22
Re: Winsock Chat Application HELP
First:
Dim loadsocket = 1
is incorrect. It won't even compile.
It's:
Dim loadsocket As Integer
loadsocket = 1
Second, it's not Load (Winsock(loadsocket)); it's Load Winsock(loadsocket)
Third, why are you trying to load a socket in the Form_Load. You don't load sockets until a connection is made in the ConnectionRequest event. Have you not read any of the posts I made?
Last edited by jmsrickland; May 22nd, 2013 at 01:04 AM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 22nd, 2013, 01:04 AM
#23
Re: Winsock Chat Application HELP
 Originally Posted by ladoo
ServerSocket(0).connect "localhost",3333 ' 3333 is your port number what ever ur using
this will connect client to your server
not sure what post 18 u trying to do sir
ServerSocket(0) is for the Server to listen on. You do not use it for connecting to "LocalHost", 3333
It's
ServerSocket(0).Listen
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 22nd, 2013, 03:35 AM
#24
Banned
Re: Winsock Chat Application HELP
 Originally Posted by jmsrickland
ServerSocket(0) is for the Server to listen on. You do not use it for connecting to "LocalHost", 3333
It's
ServerSocket(0).Listen
yeh i know buddy but his post got me confused , thought il post something rather then nothing lol
-
May 22nd, 2013, 03:38 PM
#25
Thread Starter
New Member
Re: Winsock Chat Application HELP
Sorry I don't know what I was thinking when I typed that.
First it's:
Dim loadsocket
Form_load
loadsocket = 1
End sub
In another sub,
load(winsock(loadsocket))
Oh okay, I'll try that, thank you.
-
May 22nd, 2013, 04:51 PM
#26
Re: Winsock Chat Application HELP
No! I told you in post #22 it is not
load(winsock(loadsocket))
that's why you get Type Mismatch because you are trying to make a function out of Load() and it isn't.
You have to use
load winsock(loadsocket)
instead. READ MY POSTS!
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
May 22nd, 2013, 10:14 PM
#27
Thread Starter
New Member
Re: Winsock Chat Application HELP
No, no, I understand. I was only correcting my previous post where I posted load(winsock(loadsocket))
I corrected it in my program to load winsock(loadsocket) and it is working. Something else that is strange is happening though. In order to send the text the server has I'm doing:
for x = 1 to winsock.count - 1 'because 0 is reserved for listening
winsock(x).senddata chatwindow.text
next
Now this sends it to every client connected, EXCEPT the first client that connected. But the strange thing is that if I do this:
for x = 1 to winsock.count - 1
msgbox(x)
winsock(x).senddata chatwindow.text
next
Now this sends it to ALL the clients including the first one. Why is this happening? I'm very confused.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|