Carlosky - Basic TCP Server and Client in VB.NET Console application

Поділитися
Вставка
  • Опубліковано 27 сер 2024
  • Tutorial on how to create a very basic TCP Server and Client in VB.NET Console application
    A console application cause it is quick an easy to make...
    I will make a simple one using windows forms as it was requested by McPro Griefing...
    Link to WinForms: • Carlosky - Basic TCP S...
    Source Code Downloads (VS2015):
    Console Version:
    drive.google.c...
    WinForms Version:
    drive.google.c...
    Hope you guys enjoy the tutorial... If you have any questions or requests, I'm happy to help... :)

КОМЕНТАРІ • 67

  • @rohitsahu3105
    @rohitsahu3105 3 роки тому +1

    Thanks sir

  • @eneoxkeskin
    @eneoxkeskin 2 роки тому

    hi, i cant start this project.
    I see the text attach instead of start.

  • @gameplayoffert1326
    @gameplayoffert1326 4 роки тому

    i have a question ? how to send an "array" ? or listof ?

    • @carlodasilva7839
      @carlodasilva7839  4 роки тому +1

      Hi Gameplay Offert, to send an array/list across, you'd need to Serialize it using JSON. Here is the website to find out more: www.newtonsoft.com/json/help/html/SerializingJSON.htm
      What it essentially does is creates a string from given values. But the string is structured in such a way that once it is received on the other end, we can Deserialize it back into an array/list. It is very easy to add the .dll to your project and then you will be able to convert objects/classes/arrays/lists and more into a structured string that will then be converted to Bytes and sent off. Just make sure that the newly Serialized JSON string does not exceed the buffer size of the toReceive and toSend variables. Technically using JSON is a much better method than the one I used because it provides structure and you can send a whole class and have it on the receiving end as if it were created there. If you have any trouble I am happy to help where I can. I mainly did this video to show the basics hence why I didn't use JSON. On a side note, you can also send files using this app. If you read a file using Dim toSend() As Byte = System.IO.File.ReadAllBytes("PathToFile") it will then automatically create the array with the specific size it needs. Again you just need to make sure you don't exceed the buffer sizes on both ends. When sending a file the receiving end is most likely going to run into the limit as the ReadAllBytes creates the buffer dynamically. But on the other side the toReceive is probably set to 100 000... and that might not be enough for a file...

    • @gameplayoffert1326
      @gameplayoffert1326 4 роки тому +1

      @@carlodasilva7839 oh ok thx

  • @user-it5pd1zj8o
    @user-it5pd1zj8o 6 років тому

    Plz how to using cmd commands in tcp-ip client console application

  • @gameplayoffert1326
    @gameplayoffert1326 4 роки тому

    And i have an other question ? why you enter a size on a byte ? and why the byte to send doesn't have size ? because i'm confused with that i'm sorry.

    • @carlodasilva7839
      @carlodasilva7839  4 роки тому

      So the reason you enter the size on the received bytes but not on the send bytes is because when this code executes "Endcoding.ACII.GetBytes("StringHere")" it creates a new byte array. Each character in the string "StringHere" is one byte. If you look up the data type of byte you will see it can handle values from 0 to 255. Now the byte array it creates would look like this:
      Where each byte is a character...
      83
      = S (Capital S not lower case as that would be a different number)
      116
      = t
      114
      = r
      105
      = i
      110
      = n
      103
      = g
      72
      = H (Again this number represents the Capital of H)
      101
      = e
      114
      = r
      101 = e
      I used this line to get the above byte array:
      Dim temp As Byte() = Encoding.ASCII.GetBytes("StringHere")
      So that then gets assigned to your "toSend" byte array and has a length of "10" in this case.
      Now the "toReceive" is a little tricky, because the TCP connection does not know the size of the bytes it needs to receive, so... you make a byte array of a fixed size to tell the TCP receiver that it can hold this many bytes (100000). So if you send 10 bytes worth of info to the server/client it doesn't know that it must get 10 bytes exactly, so you would set the "toReceive" to something like 100000 bytes so that you ensure that you get all 10 of the bytes you want. The rest of the bytes will just be empty spaces. That is why we then use the code:
      Dim length = ns.Read(toReceive, 0, toReceive.length)
      Dim text = Encoding.ASCII.GetString(toReceive, 0, length)
      ns.Read(...) will give us the actual length of the bytes received and put them in "toReceive" starting at index "0" to the total length of "toReceive" which is "100000".
      So it will now know that it only actually has 10 bytes of info and assign the value of "10" to "length".
      Then we use the "...GetString(...)" and provide from where we want to get the string from and provide the index of "0" and the "length" of "10" as we only want to read the first 10 bytes and not all 100000...
      There might be a better way than what I did over 5 years ago... Hahaha I am thinking of maybe remaking these videos at some point with more knowledge that I have gained throughout the years. Hope this helps.

    • @gameplayoffert1326
      @gameplayoffert1326 4 роки тому

      @@carlodasilva7839 yes he help me because know i know why i use the "Dim length = ns.Read(toReceive, 0, toReceive.length)" because i have download your project as windows form but only for the client so i have copied the windows form server code to a consoleapp and when i tried to read the "receivedbyte" he said mass space and in the mass space i don't see the text so i have added your "Dim length = ns.Read(toReceive, 0, toReceive.length)" in the console project and know i can read the received byte perfectly but i have an other question ? if i want to receive a 1gb file i need to put in "receivedbyte(1000000000)" ? if yes the size sended doesn't take more time if the file is not 1gb but 500MB or it's same ?

    • @carlodasilva7839
      @carlodasilva7839  4 роки тому

      @@gameplayoffert1326 So just to give some basics, 1 KB is 1 024 Bytes... 1 MB is 1 048 576 Bytes... 1 GB is 1 073 741 824 Bytes... And so on you keep multiplying by 1024.
      If you try to Google "gb to bytes" Google will have their converter which is a 1:1 but the standard is still 1024. They are changing the standard from 1024 to 1000 as it makes more sense... Anyway... not the issue at hand. But if you put "1000000000" in "toReceive" and you send across an actual 1 GB file (1 073 741 824), you will not get the whole file because Windows has not yet converted to the new standard yet...
      On the 500 MB topic, that's another thing, I haven't tested if "toReceive" is set to a big number and you are only sending a 20 KB file if it will take longer... But it does make sense that it would take longer because your PC needs to allocate RAM to your variable and in this case 1 GB of RAM... so allocating 20 KB should be much faster than allocating 1 GB.
      The maximum size of a byte array is 2 GB which is 2 147 483 648. but I'd suspect your connection might fail before you could send all 2 GB in one go... That's why I suggest splitting it up into pieces and sending them across and putting it back together. Hope this helps...

  • @aa-ke1ty
    @aa-ke1ty 5 років тому

    Hello the tutorial was great.But do we need internet connection to run this app ? Thanks:)

    • @carlodasilva7839
      @carlodasilva7839  5 років тому

      Hi, no you do not need Internet for this to work. You just need to make sure that any other Client is also connected to the same network as the Server. But all of it can work over the Internet when you do Port Forwarding and such...

  • @barancemsezgin4301
    @barancemsezgin4301 6 років тому

    hi, i have a question, how can i use this for video streaming to the tcp server

    • @carlodasilva7839
      @carlodasilva7839  6 років тому

      Hi Baran, I must say I haven't tried streaming but I have tried VoIP. The basic logic to it is to convert the video to a byte array and send that across in the same way you are sending the text. It needs to be a byte array anyway... So logically speaking you can also send files of any type, since it will also be a byte array. So to get the byte array from a file, you'd use System.IO.File.ReadAllBytes("C:\myFile.avi") but you will then see that this only allows for you to send a full file. But if you want it to stream, you need to put code on the receiving that then recreates the file and that allows you to be able to stream it and write to it at the same time... If you use VLC or KliteCodec, you should be able to write to the file while use one of the kodecs to stream it... I hope this helps!

    • @barancemsezgin4301
      @barancemsezgin4301 6 років тому

      thanks carlo now i try screen capture with stream it sametime.your text give hint me

    • @carlodasilva7839
      @carlodasilva7839  6 років тому

      Baran Cem Sezgin I'm glad I could help! :)

  • @Test-iz5ox
    @Test-iz5ox 8 років тому

    Hi, I really like this tutorial, however which IP do I use to make my chatroom public? Because the one you used is the LAN. I tried my public ip from sites like whatsmyip and it didnt work .-.

    • @carlodasilva7839
      @carlodasilva7839  8 років тому +1

      Hi! Yes I used my local IP in the example. And like you said, you tried getting your external IP and put it in its place... That would work fine and all, problem is your firewall/router... This in turn prevents/limits outside communication... You can watch my other video that can help you get past this hurdle... This explains what port forwarding is and how to do it... ua-cam.com/video/_eyXCcf3afw/v-deo.html... If you still can't manage, let me know and I'll try and help!

    • @Test-iz5ox
      @Test-iz5ox 8 років тому

      Yup managed to make it works, awesome dude thanks a ton :)

    • @carlodasilva7839
      @carlodasilva7839  8 років тому

      Glad I could help! Enjoy! :)

    • @MsSdsd12
      @MsSdsd12 8 років тому

      can you tell me how can I see how many connected session i have ?

    • @carlodasilva7839
      @carlodasilva7839  8 років тому

      Dim numberOfConnected As Integer = _listOfClients.Count()

  • @DiyintheGhetto
    @DiyintheGhetto 8 років тому

    Hello can you help me out I'm trying to make a client/server side application with moving picturebox for each user and have to move to new location in a panel by dragging it?

    • @carlodasilva7839
      @carlodasilva7839  8 років тому

      @Joseph
      Hi, that should be relatively easy to make... I haven't actually made one with those specs, but it can be done... I take it that you'd like me to make the project then give you the source code?

    • @DiyintheGhetto
      @DiyintheGhetto 8 років тому

      Hello no. I'm trying to learn it i need to learn vb Just lost on how to start something like that i never did a client/server application before let alone sockets and whatnot.

    • @carlodasilva7839
      @carlodasilva7839  8 років тому

      @Joseph
      Hi... Well i also made a video on how to setup a client and server using windows forms... As this one was meant to be simple and quick using a console application... ua-cam.com/video/3hfXdJj9f9s/v-deo.html... As for the picture boxes and them moving around, i could make a video on how to dynamically do that?

    • @DiyintheGhetto
      @DiyintheGhetto 8 років тому

      That would be awesome i did take a brief look at the video wow very cool. I will have to look at it some more sense its very late here. As far as the moving images go What I'm trying to do is create a user account for each user and a moving image in a panel so if they move there picturebox to a location other users that are logged in can see it moving in as well.

    • @carlodasilva7839
      @carlodasilva7839  8 років тому

      @Joseph
      Oh i see... Well that can be done, but you'll need to understand the TCP stuff well enough, cause if you want other users to see the movement, that means you will be sending the location of each picturebox to the other clients... But it is as simple as sending text in my videos, just that you will also send the coordinates of each picturebox... Here by the way, is early morning... hahaha 8:39

  • @gameplayoffert1326
    @gameplayoffert1326 4 роки тому

    and i have an other question ? can i send a file ?

    • @carlodasilva7839
      @carlodasilva7839  4 роки тому

      Yes you can send files. First you will have to get the bytes for the file. And that is very simple:
      Dim fileBytes As Byte() = System.IO.File.ReadAllBytes("C:\My\Path\Here\file.exe")
      This will add all the bytes of the file into "fileBytes" but now the tricky part is that some files can be big... So in our example we have a byte array of "100000" that equates to (100000 / 1024 = 97.65) 97.65 Kilo Bytes of data which is very very small for most files. So a solution would be to increase the size of "toReceive" right? Well yes and no... If you increase it too much, you might get errors and wouldn't compile in Visual Studio, but also if it does compile you run the risk that while sending the data across, your connection might drop depending on the quality of your network. So it is sometimes best to send sections of it at a time and not all of it. There is a lot more that I could explain, but I simply can't type it out. Try that out and if you struggle try breaking the file into pieces so that you don't have too many issues.

  • @Vexed
    @Vexed 8 років тому

    Hey, firstly thanks for this tutorial. I'm trying to make a program where the server can write a command such as; /popup and then the client will receive that packet, check if it contains /popup and go through a number of procedures which in the end result in the client creating a MsgBox containing the text that the server requested. All of this code works, I know because I originally did it so that the client writes to the server and then the server creates a MsgBox but I switched it around because obviously it needs to work the other way, but every time I write anything into the server - it instantly crashes and the client displays an error about the connection being forcibly closed. I've added Console.ReadLine() to the end of my code but this does not seem to stop it. Is there something about this function that I'm not aware of which stops this from working or something? Any help would be much appreciated! ;D

    • @carlodasilva7839
      @carlodasilva7839  8 років тому +1

      @Vexed
      I understand the problem, I will alter the current one and send you a new link to the download...

    • @Vexed
      @Vexed 8 років тому

      Carlo Da Silva Awesome, thank you very much! :)

    • @MsSdsd12
      @MsSdsd12 8 років тому

      yes can you give me such thing too.. i really wanna know how to do such things !

    • @carlodasilva7839
      @carlodasilva7839  8 років тому

      @Vexed
      Hi, sorry I haven't gotten back sooner, but haven't yet had the time to make the new version for you... But if you watch my Windows forms one, it should give you what you want. So you can connect multiple clients to one server and have them communicate with each other...
      app.box.com/s/549ajjokfz9dbjfw1gzcqn8bu4ca8qp6
      As for the server communicating back, you just need to loop thru the list of clients and send them the message from the server... You should see what I mean when you look at that video... And if you still are struggling, I will try make another video soon with a new version...

    • @carlodasilva7839
      @carlodasilva7839  8 років тому

      @MsSdsd12
      Hi, just have a look at my previous reply to @Vexed... Should help...

  • @McProGriefing1st
    @McProGriefing1st 8 років тому

    Can you make a video on how to do this on windows forms application, please.

    • @carlodasilva7839
      @carlodasilva7839  8 років тому +1

      @McPro Griefing
      Hi, thanks for the feedback, i will make a WinForms version..
      Reason i did it in a Console was just so that i didn't waste time on the interface...
      But I'll try make a short and sweet video like this one(or at least i hope it felt short and sweet... hahaha)...
      As soon as i make the video, I'll send you another reply, and I'll put a link in the description to go to the WinForms one...

    • @carlodasilva7839
      @carlodasilva7839  8 років тому +1

      @McPro Griefing
      Hi, I've finally gotten to making the WinForms version
      Carlosky - Basic TCP Server and Client in VB.NET WinForms: ua-cam.com/video/3hfXdJj9f9s/v-deo.html

    • @gabrielecozzolino6697
      @gabrielecozzolino6697 Рік тому

      @@carlodasilva7839 the drive link to the winform source code is not working anymore! Can you please fix it?

  • @Str8inyou
    @Str8inyou 7 років тому

    Would really appreciate if any1 can help me with this. thank you !

    • @carlodasilva7839
      @carlodasilva7839  7 років тому

      Hi Str8inyou, what exactly do you need help with?
      In my description I have added the source files and in this case also a link on how to do this on WinForms.

    • @Str8inyou
      @Str8inyou 7 років тому

      like , its possible to make a really chat ? I'm working on a game with my team and I wanted to make a specific chat only for us. Its possible to recive message from other IP ?

    • @carlodasilva7839
      @carlodasilva7839  7 років тому +1

      Yes that is very possible! The application I made here does exactly that! But if you want to communicate over the internet, you need to do Port Forwarding. In another one of my videos, I show how to do Port Forwarding. It actually gets rid of having to use 3rd party software like Hamachi to essentially "LAN".
      In my description of my current video, you can find the download source files.
      You then just need to change to the desired IP and you are good to go!
      How much programming knowledge do you know?
      Here is a link to my Port Forwarding Video:
      ua-cam.com/video/_eyXCcf3afw/v-deo.html

    • @Str8inyou
      @Str8inyou 7 років тому

      if u would like to add me on skype or fb would be awesome. thank you ( skype same name )

    • @carlodasilva7839
      @carlodasilva7839  7 років тому

      Hi on Skype there are 2 with that same name... Which is your background colour? Blue or Orange/Yellow?