Results 1 to 27 of 27

Thread: Winsock Chat Application HELP

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    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.

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    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?

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    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?

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    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?

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    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.

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  11. #11

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    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

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  13. #13

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    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.

  14. #14

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    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.

  15. #15
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  16. #16
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: Winsock Chat Application HELP

    Quote Originally Posted by plantinum23 View Post
    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

  17. #17

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    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

  18. #18
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  19. #19
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    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

  20. #20

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Re: Winsock Chat Application HELP

    Alright thanks guys, it's working now.

  21. #21

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    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

  22. #22
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  23. #23
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Winsock Chat Application HELP

    Quote Originally Posted by ladoo View Post
    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.

  24. #24
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: Winsock Chat Application HELP

    Quote Originally Posted by jmsrickland View Post
    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

  25. #25

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    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.

  26. #26
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  27. #27

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    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
  •  



Click Here to Expand Forum to Full Width