News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Learning how TCP and UDP work

Started by Rem, March 10, 2024, 02:37:54 PM

Previous topic - Next topic

Rem

In this ProJet, I'm interested to learn about TCP and UDP connections by making a chat room

I need help to figure a problem about the SysIPAddress32 control. I use it before in a project and it work fine.
I'm wondering if it is because I'm using it inside a tab control that cause to glitch.
I try different WS_ style options and so far no luck. I'm not sure how to fix this problem.

I need advise about the server side of my project.
So far this project has a thread to accept TCP connection and a thread created to each new client TCP connation.
My project is at the point where the server can received text from multiple clients both on TCP and UDP.

I'm curious about how a TCP server keep track of the connected clients and how the TCP server, when receiving a text from a client
is able to send this text to all the other clients?

My first impression will be something to do about the numbers of threads running minus the "accept" thread???
 
I attached my code. if you have any advise please let me know.

Thank you for your help.

Biterider

Hi Rem
If you want to have full control of the communication (TCP/UDP) you will need to go down using sockets.
Alternatively, MS provides some APIs that you can use to build a server/client application.

An example of the first approach can be found here:
https://github.com/ObjAsm/ObjAsm-C.2/tree/master/Projects/X/NetCom/Server
https://github.com/ObjAsm/ObjAsm-C.2/tree/master/Projects/X/NetCom/Client
At its core, it uses IOCP to handle all connections asynchronously.

Biterider

Vortex

#2
QuoteProgramming Tutorials
Here you can find my own tutorials.

Winsock networking tutorial (C++ or win32asm)
An introduction to networking and winsock. The tutorial was supposed to continue with more chapters but this won't happen, mainly because my own interests have shifted and I don't have the time for it. Still, it is a useful basis into the world of winsock.

Read win32asm version online
Read C++ version online

https://www.madwizard.org/programming/tutorials/

Rem

Thank you for the information.

Good to know, MS give tools to help for server/client application.
The code sample for the NetCom Server/Client is using OOP coding and im not familiar with it for now.
This is on my list that I'm interesting learn and experiment later.

The applications are using WS2_32.dll, mswsock.dll and wininet.dll.
The tools must be in the mswsock.dll and wininet.dll

When im getting more familiar using sockets i will give a look.

I read the article from Vortex. Is well done for a beginner. I learn new things on the I/O models section
There a lot more things running behind the scenes that i was expecting.

For now i interested to learn and experiment on the socket level.

Will reading the article i come up with an idea to keep tract of the open TCP connection.

Now my project, a client can send a text to every clients connected to the server.

Next step is experimenting with UDP.

Is there any advise on why I'm having issue using the SysIPAddress32 control?
I use it before when i make a IP calculator project. The only difference is this time it been use inside a tab control.

Thank you for your help.

greenozon

@Rem
some questions for the WinChat proj:

1) I built it up the .exe
2) I ran 2 copies of the app
3) one was server (to the right)
4) next one was supposed to be a client, but it hung up and started to eat 1 full CPU core

pic:




5) what are the correct steps to test the client-server model?


greenozon

Another issue is when I tried to play with server:

---------------------------
Server
---------------------------
Bind error
---------------------------
OK   
---------------------------

I guess when it's up I'd suggest to shut it down and re-create the new serfver

greenozon

Another tricky question is:
you are building against masm64 (64 bit executable)
but the WSADATA struct was defined as if it was 32 bit env...

the 64 bit on should look like:

#define WSADESCRIPTION_LEN      256
#define WSASYS_STATUS_LEN       128

typedef struct WSAData {
        WORD                    wVersion;
        WORD                    wHighVersion;
#ifdef _WIN64
        unsigned short          iMaxSockets;
        unsigned short          iMaxUdpDg;
        char FAR *              lpVendorInfo;
        char                    szDescription[WSADESCRIPTION_LEN+1];
        char                    szSystemStatus[WSASYS_STATUS_LEN+1];
#else
        char                    szDescription[WSADESCRIPTION_LEN+1];
        char                    szSystemStatus[WSASYS_STATUS_LEN+1];
        unsigned short          iMaxSockets;
        unsigned short          iMaxUdpDg;
        char FAR *              lpVendorInfo;
#endif
} WSADATA, FAR * LPWSADATA;

Rem

Hello Greenzon

Thank you for the information about the WSADATA.

I didn't know there was a version of a 64 bit WsaData structure.
So far the codes found online that i base my project was for a 32 bit.
I will try to implement the 64 bit structure to my code.

That was the glitch i was talking about the SysIPAddress32 control.
On the "Client Connection" tab, if you enter the port number fist then you can enter the IP address.
But if you click directly on the SysIPAddress32 control it will hang.

On the "Server Connection" tab, if you click twice on the connection you will have the "bind error"
You need to Disconnect before try an other connection.

This project is a work in progress..

Rem

Hello team

Is has been a few year that i didn't program in assembly. My assembly is very rusted.

I found out what was the problem about the control SysIPAddress32.
What was wrong is the order of the parameters in the resource file

Instate of
CONTROL "", IDC_IP, "SysIPAddress32", 0, 60, 40, 70, 12, WS_TABSTOP | WS_CHILD

it should be
CONTROL "", IDC_IP, "SysIPAddress32", WS_TABSTOP | WS_CHILD, 60, 40, 70, 12

My project is getting along.
I'm using a ListView control for the Clients names and connections.

Somehow I'm having an issue to display the text in the Listview header.

With TASM5.0 i used the code "mov lvc.pszText, offset szLstvHeader1"

I saw some x64 examples using the lines
mov eax, offset szLstvHeader1
...
mov lvc.pszText, rax

I try lea, rbx, szLstvHeader1
...
mov lvc.pszText, rbx

I read to use the option LVS_EX_DOUBLEBUFFER when using a ListView control in x64.

So far no success to display the text in the Lisview header.
I'm wondering what I'm doing wrong...

This is the code im using now.

OnInitChatDialogProc proc hDlg:qword
local   lvc : LVCOLUMNA

   invoke   CreateFont, 14, 0, 0, 0, FW_BOLD, 0, 0, 0, 0, 0, 0, 0, 0, addr szFontName
   mov r12d, eax
   
   invoke  GetDlgItem, hDlg, IDC_CLTUSERLIST
   mov   hCltUserListV, eax

   invoke   SendMessage, hCltUserListV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_GRIDLINES or LVS_EX_FULLROWSELECT or LVS_EX_DOUBLEBUFFER   

   mov ebx, offset szLstvHeader1
   
   mov lvc._mask, LVCF_TEXT or LVCF_WIDTH
   mov lvc.pszText, rbx
   mov lvc._cx, 154
   mov lvc.iSubItem, 0
   invoke   SendMessage, hCltUserListV, LVM_INSERTCOLUMNA, 0, addr lvc

   lea rbx, szLstvHeader2
   
   mov lvc._mask, LVCF_TEXT or LVCF_WIDTH
   mov lvc.pszText, rbx
   mov lvc._cx, 34
   mov lvc.iSubItem, 1
   invoke   SendMessage, hCltUserListV, LVM_INSERTCOLUMNA, 1, addr lvc
   ...

If someone know what I'm doing wrong, please let me know.

Thank you for your help.

sinsi

Check the return size of functions that return a value, handles and addresses will be in RAX
   invoke   CreateFont, 14, 0, 0, 0, FW_BOLD, 0, 0, 0, 0, 0, 0, 0, 0, addr szFontName
   mov r12d, eax ;<<<<<<<<<<<<< should be RAX since it's a HANDLE
   
   invoke  GetDlgItem, hDlg, IDC_CLTUSERLIST
   mov   hCltUserListV, eax ;<<<<<<<<<<<<< should be RAX since it's a HANDLE

   invoke   SendMessage, hCltUserListV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_GRIDLINES or LVS_EX_FULLROWSELECT or LVS_EX_DOUBLEBUFFER   

   mov ebx, offset szLstvHeader1 ;<<<<<<<<<<<<< should be RBX since it's an OFFSET
                                 ;<<<<<<<<<<<<< consider using LEA with 64-bit code
   
   mov lvc._mask, LVCF_TEXT or LVCF_WIDTH
   mov lvc.pszText, rbx ;;<<<<<<<<<<<<< address is probably wrong due to using EBX above

NoCforMe

Which brings up my question: Are you sure you want to use 64-bit code? Is that a requirement for this usage?

I ask because 64-bit is a bit more complex than 32-bit (but not twice!), but there are also more "gotchas". I know 64-bit is all the rage, but I still use 32-bit exclusively and it suits me fine.

Not trying to talk out of it necessarily but wondering if you really want to go that route.
Assembly language programming should be fun. That's why I do it.

alCoPaUL

pretty sure you have access to visual studio full version but as early as visual studio .net 2002, there is a solid example dealing with your current project (and it's even in the MFC examples, pretty sure there are similar in the psdk section)

Rem

Hello team

After doing research a found a line that fix my issue of not showing the text on the Listview header, and hope this information can help.

In the win64.inc file a line need to be added in the LVCOLUMNA structure. That make me wonder if i have the latest win64.inc file...

LVCOLUMNA STRUCT
    _mask dd ?
    fmt dd ?
    _cx dd ?
align 8      ; line added to show the text in Listview header.
    pszText dq ?
    cchTextMax dd ?
    iSubItem dd ?
    iImage dd ?
    iOrder dd ?
    cxMin dd ?
    cxDefault dd ?
    cxIdeal dd ?
LVCOLUMNA ENDS

NoCforMe, i understand that programing in x64 is not a requirement.
Since it has been few years that i didn't program in assembly, I'm curious to learn the difference between the 32bit and 64bit programming.
I would like to do new projects and redo some of my old TASM5.0 projects

sinsi, Thank you for the tip, i will remember to use "lea" instate "offset".
I'm just beginning programming in x64, still have the habit to used the 32bit variable.

I learn to work with TCP and UDP connection. send text to selected clients. This ProJet is done.

NoCforMe

Quote from: Rem on June 06, 2024, 11:11:48 AMNoCforMe, i understand that programing in x64 is not a requirement.
Since it has been few years that i didn't program in assembly, I'm curious to learn the difference between the 32bit and 64bit programming.
Wellll, I can tell you for one thing that 64-bit progamming is a big pain in the ass compared to 32-bit, at least so far as using the packages here (MASM32/MASM64).

The 32-bit MASM package here is very well developed and tested; the 64-bit one not so much, as many other members here can tell you. And there are a lot more "gotchas" in 64-bit territory, as you're discovering.

That's why I suggest that you not use 64-bit programming, unless there's some really compelling reason to do so.
Assembly language programming should be fun. That's why I do it.