C
C#10mo ago
zoon_

✅ winform sockets

No description
205 Replies
zoon_
zoon_OP10mo ago
So i managed to get the server to show messages on the client's chatbox but if i open a new instance of my application it doesnt work and it only works on the one opened.. the one i started the server from any ideas how to make it work on all connected clients ? heres the server code
zoon_
zoon_OP10mo ago
leowest
leowest10mo ago
can you use $paste instead?
MODiX
MODiX10mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
leowest
leowest10mo ago
discord embed files are really bad
zoon_
zoon_OP10mo ago
okay
zoon_
zoon_OP10mo ago
BlazeBin - igffswgoscoo
A tool for sharing your source code with the world!
zoon_
zoon_OP10mo ago
here are all the 3 files
leowest
leowest10mo ago
k give me a few to look
zoon_
zoon_OP10mo ago
okie
leowest
leowest10mo ago
im a bit confused ngl, normally, what u would have is for example
public class Client
{
public int Id {get;set;}
public Socket Socket {get;set;}
}
public class Client
{
public int Id {get;set;}
public Socket Socket {get;set;}
}
And that would be your list of clients. Then when u receive a message from say client 1, u know that u need to broadcast that messge to all other clients except for 1 but what you have in your server does not make much sense if feels like you created 2 separated apps then u copy pasted the client side of it into the app and you're not injecting an instance of the clienthome in order to update it, but the client home doesn't exist in your server and u also have a clients list thatu dont add items to
zoon_
zoon_OP10mo ago
hmmm
leowest
leowest10mo ago
does this make sense to u?
zoon_
zoon_OP10mo ago
i tried to have a client class before
leowest
leowest10mo ago
like for example
zoon_
zoon_OP10mo ago
but i had a problem where i couldnt access the tb_chatbox in my form to broadcast the message on it
leowest
leowest10mo ago
while(flag)
{
serverSocket.Listen(0);
Socket clientSocket = serverSocket.Accept();
// client connected, so lets add it to the list of clients
var client = new Client { Id = number, Socket = clientSocket };
clients.Add(client);
// now lets pass over the client to receive its messages
Thread clientThread = new Thread(() => clientConnection(client));
clientThread.Start();
number++;
}
while(flag)
{
serverSocket.Listen(0);
Socket clientSocket = serverSocket.Accept();
// client connected, so lets add it to the list of clients
var client = new Client { Id = number, Socket = clientSocket };
clients.Add(client);
// now lets pass over the client to receive its messages
Thread clientThread = new Thread(() => clientConnection(client));
clientThread.Start();
number++;
}
this is just an example ok. So now when you're receiving messges u know from which client and u have the client socket to reuse later to send messages back because every time a client connects u add it to the clients list
zoon_
zoon_OP10mo ago
yeah but how will i show the message onto the client's chatbox ? can i use serverSocket.send ? it will send to all connected clients ?
leowest
leowest10mo ago
so then u would have say
private void OnReceiveMessage(Client sender, string message)
{
foreach(var client in clients)
{
if (client.Id == sender.Id) continue;
client.Socket.Send(message);
}
}
private void OnReceiveMessage(Client sender, string message)
{
foreach(var client in clients)
{
if (client.Id == sender.Id) continue;
client.Socket.Send(message);
}
}
exactly
zoon_
zoon_OP10mo ago
okay another question.. what if i open an instance of my app then open another one
leowest
leowest10mo ago
you probably dont want to do that, but that is just as an example
zoon_
zoon_OP10mo ago
and open the client side and connect.. will it connect to the same server of the first instance ? why not ?
leowest
leowest10mo ago
u probably want to save the messages in a queue to send them because u would have multiple clients sending messages at once that is referring to my last code
zoon_
zoon_OP10mo ago
i see
leowest
leowest10mo ago
that is just illustrating how u would do a broadcast excluding who sent the message
zoon_
zoon_OP10mo ago
hmm let me try and check back with u
leowest
leowest10mo ago
yes they would connect to the same server what your first loop does is, it waits for a client, then once one connects it will add it to the list, and then pass the client to a loop to process its messages and then go back to waiting for new clients so if u open multiple clients to connect to that ip and port they would all connect to it
zoon_
zoon_OP10mo ago
i see
leowest
leowest10mo ago
u also need to include code in your client to receive messages
zoon_
zoon_OP10mo ago
wdym ? cant i receive messages in my clienthomepage ?
leowest
leowest10mo ago
well your client only have code to send messages
zoon_
zoon_OP10mo ago
yes it should have code to receive messages from the server also right ? and put it on the chatbox
leowest
leowest10mo ago
yes
zoon_
zoon_OP10mo ago
do i get reference to the current clienthomepage in the client class ? like this wait
leowest
leowest10mo ago
no getting references if for when its something inside the same application but your clients need to run independent of the existence of the server all they should know if the ip and port so its a completely separated application
zoon_
zoon_OP10mo ago
i see
leowest
leowest10mo ago
so u create a project for Client, u create a project for Server
zoon_
zoon_OP10mo ago
so they cant be in the same project but differetn forms ? or what
leowest
leowest10mo ago
they could but you would require code to know which one specifically to start u cannot start a server and a client everytime u create a client does that make sense?
zoon_
zoon_OP10mo ago
yes ofc
leowest
leowest10mo ago
what people normally do is they create a library that contains both the client and server code and then u share that library in both projects and u only initialize what u need, i.e.: the client or the server
zoon_
zoon_OP10mo ago
i see
leowest
leowest10mo ago
because often the client and server have common things
zoon_
zoon_OP10mo ago
okay thats too much work for a simple project
leowest
leowest10mo ago
yeah
zoon_
zoon_OP10mo ago
i just want to get this broadcasting thingy to work first and then figure all of this out
leowest
leowest10mo ago
just create 2 projects one for client and one for server
zoon_
zoon_OP10mo ago
so what you said i should do is keep a list of clients in a list and when i loop on them i send a message from the server and they receive it ?
leowest
leowest10mo ago
yes u create a list of clients when they connect to the server u add them to the list when they disconnect u remove them
zoon_
zoon_OP10mo ago
yes how would i receive the message ? actually.. what do i do after i receive it ?
zoon_
zoon_OP10mo ago
No description
zoon_
zoon_OP10mo ago
No description
zoon_
zoon_OP10mo ago
??
leowest
leowest10mo ago
u dont need any code inside your Client class for now just keep it simple
zoon_
zoon_OP10mo ago
i see alright
leowest
leowest10mo ago
u loop the client class just like u did, and u use the Socket to send the message
zoon_
zoon_OP10mo ago
i use the serversocket ? or the client socket ?
leowest
leowest10mo ago
the client
zoon_
zoon_OP10mo ago
but shouldnt the client socket receive only ?
leowest
leowest10mo ago
because u want to send it to the clients
zoon_
zoon_OP10mo ago
oh wait so when i do clientsocket.Send() im sending to the clientsocket ?
leowest
leowest10mo ago
both the client and server sockets have 1 stream for receiving and 1 for sending
zoon_
zoon_OP10mo ago
or sending FROM the client socket ?
leowest
leowest10mo ago
yes you're using the connection the server have for that specific client to send data back that's why u need the list so u keep track of all the clients
zoon_
zoon_OP10mo ago
?
No description
leowest
leowest10mo ago
u probably dont want that message.box there because it u have 100 clients it will pop 100 times :catlaugh:
zoon_
zoon_OP10mo ago
yeah i know its just for debugging and the client should have a thread for listening ?
leowest
leowest10mo ago
yes
zoon_
zoon_OP10mo ago
or accepting i mean
leowest
leowest10mo ago
for reading
zoon_
zoon_OP10mo ago
yes
leowest
leowest10mo ago
and it looks exact like the one u have for the server
zoon_
zoon_OP10mo ago
oh really even the clientSocket.listen?
leowest
leowest10mo ago
no
zoon_
zoon_OP10mo ago
yeah i figured
leowest
leowest10mo ago
just what is inside of clientConnection plus the thread
zoon_
zoon_OP10mo ago
wow it worked soo my main issue is that i didnt think of using the server to send back messages lmao
leowest
leowest10mo ago
yep you were creating new clients a new forms in your own server code and adding references of it to the sever and trying to update that so it was never an actual server and client
zoon_
zoon_OP10mo ago
yeah i guess so does the \n not work in textboxes ? its not creating a new line
leowest
leowest10mo ago
Environment.NewLine
zoon_
zoon_OP10mo ago
when i close the client form it says socket was forcibly closed it throws an exception
zoon_
zoon_OP10mo ago
No description
zoon_
zoon_OP10mo ago
throws an exception on this line
No description
zoon_
zoon_OP10mo ago
in the bytesRead line would checking if the socket is connected fix it ?
leowest
leowest10mo ago
yes because the server cannot know the client left if the client does not send a message saying its leaving
zoon_
zoon_OP10mo ago
nvm
No description
leowest
leowest10mo ago
nah u have to try and catch for socket exception and handle it
zoon_
zoon_OP10mo ago
i did oh nvm
leowest
leowest10mo ago
so when that happens u want to try and catch the socket exception and then remove the client as its no longer connected
zoon_
zoon_OP10mo ago
No description
zoon_
zoon_OP10mo ago
here it closed but its throwing exceptions in message boxes
leowest
leowest10mo ago
because u told it to write the error to messageboxes
zoon_
zoon_OP10mo ago
i want to change this label to be the client number
No description
zoon_
zoon_OP10mo ago
how can i do that ?
leowest
leowest10mo ago
u would have to send a message to the client telling him which number he is
zoon_
zoon_OP10mo ago
i see what if i want to make a name for each client and the client enters his name
leowest
leowest10mo ago
sure and how would the server know that name? the answer is simple
zoon_
zoon_OP10mo ago
how ?
leowest
leowest10mo ago
come on u know the answer
zoon_
zoon_OP10mo ago
it wont ?
leowest
leowest10mo ago
then what u do? u tell him
zoon_
zoon_OP10mo ago
i dont bother doing it 💀
leowest
leowest10mo ago
and how do u tell him? by sending a message 😛
zoon_
zoon_OP10mo ago
through a message i see 😂
leowest
leowest10mo ago
everything goes thru messages at this point
zoon_
zoon_OP10mo ago
i see is that the correct way of doing stuff
leowest
leowest10mo ago
the only thing the server knows about you is the ip and port u connected to it
zoon_
zoon_OP10mo ago
i see
leowest
leowest10mo ago
well the more complex your messages gets u at some point create a layout for your messages
zoon_
zoon_OP10mo ago
yeah i see
leowest
leowest10mo ago
so before u were just sending text
zoon_
zoon_OP10mo ago
ill send an object
leowest
leowest10mo ago
size
action type
action to perform
size
action type
action to perform
where size would be the size of the message action type we normally call this opcode its an operator that tells how to handle it so imagine an enum
public enum ActionType
{
Login,
Logout,
ChangeName,
Message,
}
public enum ActionType
{
Login,
Logout,
ChangeName,
Message,
}
zoon_
zoon_OP10mo ago
i seee smart way
leowest
leowest10mo ago
so now we have 4 possible actions, we can exchange a login with the client where it could tell me its username or we can logout to tell the server we are safely disconnecting or we could changename or send a message and then action to perform would be say the message u want to send if ur type was a message or the username to update etc and now how u receive your messages have also changed
zoon_
zoon_OP10mo ago
things have gotten big all of a sudden this is good to know but ill keep it simple for now
leowest
leowest10mo ago
yeah I mean u can even do this with strings for example MESSAGE|writemessage here USERNAME|sendusername and it would be as simple as parsing the text i.e.:
zoon_
zoon_OP10mo ago
i see i see interesting and i can split it by the |
leowest
leowest10mo ago
var parts = message.Split('|');
switch (parts[0])
{
case "MESSAGE":
// do this
break;
}
var parts = message.Split('|');
switch (parts[0])
{
case "MESSAGE":
// do this
break;
}
zoon_
zoon_OP10mo ago
yeah fair enough yk what ill try and do this
zoon_
zoon_OP10mo ago
No description
zoon_
zoon_OP10mo ago
like this for example ?
leowest
leowest10mo ago
sure
zoon_
zoon_OP10mo ago
No description
zoon_
zoon_OP10mo ago
does that seem right ? now if i want i can extend it to any type of message i want right ?
leowest
leowest10mo ago
sure
zoon_
zoon_OP10mo ago
also if i have this app on another computer and i connect to the same ip and port will it be on the same server ? or no
zoon_
zoon_OP10mo ago
what the hell 💀
No description
leowest
leowest10mo ago
assuming your binding to a external ip and your firewall allows the port and you have nothing else stopping connections to it sure
zoon_
zoon_OP10mo ago
i see what even is that why is the message appearing like that
leowest
leowest10mo ago
sounds like its reading wrong
zoon_
zoon_OP10mo ago
No description
leowest
leowest10mo ago
either ur encoding or the sizes
zoon_
zoon_OP10mo ago
this messagebox.show showed the message fine
leowest
leowest10mo ago
so its how ur updating the textboxes then
zoon_
zoon_OP10mo ago
No description
zoon_
zoon_OP10mo ago
nvm i fixed it if i create a new form for client login to enter his name and password etc do i have to create a new socket or smth ?
leowest
leowest10mo ago
no u just send a message
zoon_
zoon_OP10mo ago
yeah how do i send a message without an identified serversocket
leowest
leowest10mo ago
its not without you connect then u send your username
AUTHENTICATION|username:password
AUTHENTICATION|username:password
for example give you're sending it all in plaintext
zoon_
zoon_OP10mo ago
yes but in order to connect i need to initialize a socket a client socket
leowest
leowest10mo ago
yes u do
zoon_
zoon_OP10mo ago
i already have a socket in the clienthomepage
No description
zoon_
zoon_OP10mo ago
does that mean i have to delete that socket and create it in the login form ?
leowest
leowest10mo ago
and that u do when u click connect do u not?
No description
zoon_
zoon_OP10mo ago
yes exactly i create the socket when i press connect
leowest
leowest10mo ago
ah I see what u mean you could define the socket on Program.cs and pass it as reference to your forms or u could just make your chat form open a login dialog either way u need to connect first in order to send or receive or exchange anything
zoon_
zoon_OP10mo ago
yeah and making multiple client sockets for one app would be dumb right ?
leowest
leowest10mo ago
for the login purpose yeah it would make no sense at the very least not in your current context and what ur doing
zoon_
zoon_OP10mo ago
hey i know this has nothing to do with code but
No description
zoon_
zoon_OP10mo ago
what am i supposed to do here 💀 how do i accept the pull request
leowest
leowest10mo ago
im not sure what you're doing looks like you're working on a different branch
zoon_
zoon_OP10mo ago
yeah i want to change to the main branch idk why its set to master but i already uploaded the folders to master is there a way to put them on main too and remove them from master ?
leowest
leowest10mo ago
well u compare and send a pull request
zoon_
zoon_OP10mo ago
yeah im trying to do that but idk where to click tbh
leowest
leowest10mo ago
that would send a PR to your main that u can review and accept the green button?
zoon_
zoon_OP10mo ago
i did press that
leowest
leowest10mo ago
then u see if the changes are right and then there is a submit pull request somewhere I dont use github via the web
zoon_
zoon_OP10mo ago
also what is the master branch for and the main branch for ? what do u use ?
leowest
leowest10mo ago
normally you start with a main branch visual studio and at some point u create or changed to another branch I wouldn't know why because I dont know what u did visual studio can pull push view changes and awhole lot of things
zoon_
zoon_OP10mo ago
ohh im just asking whats the difference between master and main
leowest
leowest10mo ago
main is the main repository master is a branch off of it so if u want to test some new code u create a branch work there test it once u see its good enough u submit a PR to main or w/e
zoon_
zoon_OP10mo ago
i see i see
zoon_
zoon_OP10mo ago
thing is
No description
zoon_
zoon_OP10mo ago
its not letting me make any pull request
leowest
leowest10mo ago
are u managing your whole code and submiting it via github web? or command line or visual studio
zoon_
zoon_OP10mo ago
git bash
leowest
leowest10mo ago
ok what were the last few lines u sent there?
zoon_
zoon_OP10mo ago
git commit -m git push origin master
leowest
leowest10mo ago
and did u add an origin to github
zoon_
zoon_OP10mo ago
git remote add origin yeah
leowest
leowest10mo ago
why did u do git push origin master its just git push
zoon_
zoon_OP10mo ago
because im already on master for some reason i forgot to change to main
leowest
leowest10mo ago
leowest
leowest10mo ago
just for lil fun
zoon_
zoon_OP10mo ago
💀 just for lil fun and im over here ssuffering to make smth like this 🤣 hey btw when you try to put your code on github do u use git or what do u use ?
leowest
leowest10mo ago
visual studio
zoon_
zoon_OP10mo ago
code or community ?
leowest
leowest10mo ago
community ofc I dont use code for c# from the solution explorer u can commit
zoon_
zoon_OP10mo ago
but in community you can only create repos right ?
leowest
leowest10mo ago
push, pull etc
zoon_
zoon_OP10mo ago
what if u want to add new projects to an existing repo
leowest
leowest10mo ago
no when u create a solution u have the option to add git to the solution by adding it to the solution it will track all the projects within it
zoon_
zoon_OP10mo ago
oh can i still do that now also will it upload the files or the folder ?
leowest
leowest10mo ago
I suggest you open a new #help for that I am not entirely sure how u added git to your folder and if visual studio would be able to track it but if u create a new project or solution at the bottom right u should see this
leowest
leowest10mo ago
No description
zoon_
zoon_OP10mo ago
alrighty
leowest
leowest10mo ago
clicking on it gives u some options
No description
zoon_
zoon_OP10mo ago
its showing local repos
leowest
leowest10mo ago
and then u should see everything on the solution explorer
zoon_
zoon_OP10mo ago
alright thank you bro
leowest
leowest10mo ago
no worries
leowest
leowest10mo ago
No description
leowest
leowest10mo ago
u see the red, it means I changed something
zoon_
zoon_OP10mo ago
can i put new folders here and commit and push all of them through visual studio ?
No description
leowest
leowest10mo ago
No description
leowest
leowest10mo ago
No description
leowest
leowest10mo ago
u also have a tab dedicated to it in the solution explorer
zoon_
zoon_OP10mo ago
ii see i see alrighty
leowest
leowest10mo ago
I dont know how u setup your git so I can't help with that sorry im not a git expert ;P
zoon_
zoon_OP10mo ago
i see
leowest
leowest10mo ago
that's why I suggested u to open a new #help I can only show u what it looks on visual studio
zoon_
zoon_OP10mo ago
i think ill check a video out
leowest
leowest10mo ago
alright, anyway im about to head off, if u dont have any more questions about the socket project feel free to mark the thread as solved by doing a /close
zoon_
zoon_OP10mo ago
alright cheers bro
Want results from more Discord servers?
Add your server