C
C#4mo ago
Ip Man

✅ winform sockets

No description
205 Replies
Ip Man
Ip Man4mo 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
leowest
leowest4mo ago
can you use $paste instead?
MODiX
MODiX4mo 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
leowest4mo ago
discord embed files are really bad
Ip Man
Ip Man4mo ago
okay
Ip Man
Ip Man4mo ago
BlazeBin - igffswgoscoo
A tool for sharing your source code with the world!
Ip Man
Ip Man4mo ago
here are all the 3 files
leowest
leowest4mo ago
k give me a few to look
Ip Man
Ip Man4mo ago
okie
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
hmmm
leowest
leowest4mo ago
does this make sense to u?
Ip Man
Ip Man4mo ago
i tried to have a client class before
leowest
leowest4mo ago
like for example
Ip Man
Ip Man4mo ago
but i had a problem where i couldnt access the tb_chatbox in my form to broadcast the message on it
leowest
leowest4mo 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
Ip Man
Ip Man4mo 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
leowest4mo 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
Ip Man
Ip Man4mo ago
okay another question.. what if i open an instance of my app then open another one
leowest
leowest4mo ago
you probably dont want to do that, but that is just as an example
Ip Man
Ip Man4mo ago
and open the client side and connect.. will it connect to the same server of the first instance ? why not ?
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
i see
leowest
leowest4mo ago
that is just illustrating how u would do a broadcast excluding who sent the message
Ip Man
Ip Man4mo ago
hmm let me try and check back with u
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
i see
leowest
leowest4mo ago
u also need to include code in your client to receive messages
Ip Man
Ip Man4mo ago
wdym ? cant i receive messages in my clienthomepage ?
leowest
leowest4mo ago
well your client only have code to send messages
Ip Man
Ip Man4mo ago
yes it should have code to receive messages from the server also right ? and put it on the chatbox
leowest
leowest4mo ago
yes
Ip Man
Ip Man4mo ago
do i get reference to the current clienthomepage in the client class ? like this wait
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
i see
leowest
leowest4mo ago
so u create a project for Client, u create a project for Server
Ip Man
Ip Man4mo ago
so they cant be in the same project but differetn forms ? or what
leowest
leowest4mo 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?
Ip Man
Ip Man4mo ago
yes ofc
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
i see
leowest
leowest4mo ago
because often the client and server have common things
Ip Man
Ip Man4mo ago
okay thats too much work for a simple project
leowest
leowest4mo ago
yeah
Ip Man
Ip Man4mo ago
i just want to get this broadcasting thingy to work first and then figure all of this out
leowest
leowest4mo ago
just create 2 projects one for client and one for server
Ip Man
Ip Man4mo 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
leowest4mo 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
Ip Man
Ip Man4mo ago
yes how would i receive the message ? actually.. what do i do after i receive it ?
Ip Man
Ip Man4mo ago
No description
Ip Man
Ip Man4mo ago
No description
Ip Man
Ip Man4mo ago
??
leowest
leowest4mo ago
u dont need any code inside your Client class for now just keep it simple
Ip Man
Ip Man4mo ago
i see alright
leowest
leowest4mo ago
u loop the client class just like u did, and u use the Socket to send the message
Ip Man
Ip Man4mo ago
i use the serversocket ? or the client socket ?
leowest
leowest4mo ago
the client
Ip Man
Ip Man4mo ago
but shouldnt the client socket receive only ?
leowest
leowest4mo ago
because u want to send it to the clients
Ip Man
Ip Man4mo ago
oh wait so when i do clientsocket.Send() im sending to the clientsocket ?
leowest
leowest4mo ago
both the client and server sockets have 1 stream for receiving and 1 for sending
Ip Man
Ip Man4mo ago
or sending FROM the client socket ?
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
?
No description
leowest
leowest4mo ago
u probably dont want that message.box there because it u have 100 clients it will pop 100 times :catlaugh:
Ip Man
Ip Man4mo ago
yeah i know its just for debugging and the client should have a thread for listening ?
leowest
leowest4mo ago
yes
Ip Man
Ip Man4mo ago
or accepting i mean
leowest
leowest4mo ago
for reading
Ip Man
Ip Man4mo ago
yes
leowest
leowest4mo ago
and it looks exact like the one u have for the server
Ip Man
Ip Man4mo ago
oh really even the clientSocket.listen?
leowest
leowest4mo ago
no
Ip Man
Ip Man4mo ago
yeah i figured
leowest
leowest4mo ago
just what is inside of clientConnection plus the thread
Ip Man
Ip Man4mo ago
wow it worked soo my main issue is that i didnt think of using the server to send back messages lmao
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
yeah i guess so does the \n not work in textboxes ? its not creating a new line
leowest
leowest4mo ago
Environment.NewLine
Ip Man
Ip Man4mo ago
when i close the client form it says socket was forcibly closed it throws an exception
Ip Man
Ip Man4mo ago
No description
Ip Man
Ip Man4mo ago
throws an exception on this line
No description
Ip Man
Ip Man4mo ago
in the bytesRead line would checking if the socket is connected fix it ?
leowest
leowest4mo ago
yes because the server cannot know the client left if the client does not send a message saying its leaving
Ip Man
Ip Man4mo ago
nvm
No description
leowest
leowest4mo ago
nah u have to try and catch for socket exception and handle it
Ip Man
Ip Man4mo ago
i did oh nvm
leowest
leowest4mo ago
so when that happens u want to try and catch the socket exception and then remove the client as its no longer connected
Ip Man
Ip Man4mo ago
No description
Ip Man
Ip Man4mo ago
here it closed but its throwing exceptions in message boxes
leowest
leowest4mo ago
because u told it to write the error to messageboxes
Ip Man
Ip Man4mo ago
i want to change this label to be the client number
No description
Ip Man
Ip Man4mo ago
how can i do that ?
leowest
leowest4mo ago
u would have to send a message to the client telling him which number he is
Ip Man
Ip Man4mo ago
i see what if i want to make a name for each client and the client enters his name
leowest
leowest4mo ago
sure and how would the server know that name? the answer is simple
Ip Man
Ip Man4mo ago
how ?
leowest
leowest4mo ago
come on u know the answer
Ip Man
Ip Man4mo ago
it wont ?
leowest
leowest4mo ago
then what u do? u tell him
Ip Man
Ip Man4mo ago
i dont bother doing it 💀
leowest
leowest4mo ago
and how do u tell him? by sending a message 😛
Ip Man
Ip Man4mo ago
through a message i see 😂
leowest
leowest4mo ago
everything goes thru messages at this point
Ip Man
Ip Man4mo ago
i see is that the correct way of doing stuff
leowest
leowest4mo ago
the only thing the server knows about you is the ip and port u connected to it
Ip Man
Ip Man4mo ago
i see
leowest
leowest4mo ago
well the more complex your messages gets u at some point create a layout for your messages
Ip Man
Ip Man4mo ago
yeah i see
leowest
leowest4mo ago
so before u were just sending text
Ip Man
Ip Man4mo ago
ill send an object
leowest
leowest4mo 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,
}
Ip Man
Ip Man4mo ago
i seee smart way
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
things have gotten big all of a sudden this is good to know but ill keep it simple for now
leowest
leowest4mo 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.:
Ip Man
Ip Man4mo ago
i see i see interesting and i can split it by the |
leowest
leowest4mo 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;
}
Ip Man
Ip Man4mo ago
yeah fair enough yk what ill try and do this
Ip Man
Ip Man4mo ago
No description
Ip Man
Ip Man4mo ago
like this for example ?
leowest
leowest4mo ago
sure
Ip Man
Ip Man4mo ago
No description
Ip Man
Ip Man4mo ago
does that seem right ? now if i want i can extend it to any type of message i want right ?
leowest
leowest4mo ago
sure
Ip Man
Ip Man4mo 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
Ip Man
Ip Man4mo ago
what the hell 💀
No description
leowest
leowest4mo ago
assuming your binding to a external ip and your firewall allows the port and you have nothing else stopping connections to it sure
Ip Man
Ip Man4mo ago
i see what even is that why is the message appearing like that
leowest
leowest4mo ago
sounds like its reading wrong
Ip Man
Ip Man4mo ago
No description
leowest
leowest4mo ago
either ur encoding or the sizes
Ip Man
Ip Man4mo ago
this messagebox.show showed the message fine
leowest
leowest4mo ago
so its how ur updating the textboxes then
Ip Man
Ip Man4mo ago
No description
Ip Man
Ip Man4mo 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
leowest4mo ago
no u just send a message
Ip Man
Ip Man4mo ago
yeah how do i send a message without an identified serversocket
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
yes but in order to connect i need to initialize a socket a client socket
leowest
leowest4mo ago
yes u do
Ip Man
Ip Man4mo ago
i already have a socket in the clienthomepage
No description
Ip Man
Ip Man4mo ago
does that mean i have to delete that socket and create it in the login form ?
leowest
leowest4mo ago
and that u do when u click connect do u not?
No description
Ip Man
Ip Man4mo ago
yes exactly i create the socket when i press connect
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
yeah and making multiple client sockets for one app would be dumb right ?
leowest
leowest4mo ago
for the login purpose yeah it would make no sense at the very least not in your current context and what ur doing
Ip Man
Ip Man4mo ago
hey i know this has nothing to do with code but
No description
Ip Man
Ip Man4mo ago
what am i supposed to do here 💀 how do i accept the pull request
leowest
leowest4mo ago
im not sure what you're doing looks like you're working on a different branch
Ip Man
Ip Man4mo 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
leowest4mo ago
well u compare and send a pull request
Ip Man
Ip Man4mo ago
yeah im trying to do that but idk where to click tbh
leowest
leowest4mo ago
that would send a PR to your main that u can review and accept the green button?
Ip Man
Ip Man4mo ago
i did press that
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
also what is the master branch for and the main branch for ? what do u use ?
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
ohh im just asking whats the difference between master and main
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
i see i see
Ip Man
Ip Man4mo ago
thing is
No description
Ip Man
Ip Man4mo ago
its not letting me make any pull request
leowest
leowest4mo ago
are u managing your whole code and submiting it via github web? or command line or visual studio
Ip Man
Ip Man4mo ago
git bash
leowest
leowest4mo ago
ok what were the last few lines u sent there?
Ip Man
Ip Man4mo ago
git commit -m git push origin master
leowest
leowest4mo ago
and did u add an origin to github
Ip Man
Ip Man4mo ago
git remote add origin yeah
leowest
leowest4mo ago
why did u do git push origin master its just git push
Ip Man
Ip Man4mo ago
because im already on master for some reason i forgot to change to main
leowest
leowest4mo ago
leowest
leowest4mo ago
just for lil fun
Ip Man
Ip Man4mo 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
leowest4mo ago
visual studio
Ip Man
Ip Man4mo ago
code or community ?
leowest
leowest4mo ago
community ofc I dont use code for c# from the solution explorer u can commit
Ip Man
Ip Man4mo ago
but in community you can only create repos right ?
leowest
leowest4mo ago
push, pull etc
Ip Man
Ip Man4mo ago
what if u want to add new projects to an existing repo
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
oh can i still do that now also will it upload the files or the folder ?
leowest
leowest4mo 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
leowest4mo ago
No description
Ip Man
Ip Man4mo ago
alrighty
leowest
leowest4mo ago
clicking on it gives u some options
No description
Ip Man
Ip Man4mo ago
its showing local repos
leowest
leowest4mo ago
and then u should see everything on the solution explorer
Ip Man
Ip Man4mo ago
alright thank you bro
leowest
leowest4mo ago
no worries
leowest
leowest4mo ago
No description
leowest
leowest4mo ago
u see the red, it means I changed something
Ip Man
Ip Man4mo ago
can i put new folders here and commit and push all of them through visual studio ?
No description
leowest
leowest4mo ago
No description
leowest
leowest4mo ago
No description
leowest
leowest4mo ago
u also have a tab dedicated to it in the solution explorer
Ip Man
Ip Man4mo ago
ii see i see alrighty
leowest
leowest4mo ago
I dont know how u setup your git so I can't help with that sorry im not a git expert ;P
Ip Man
Ip Man4mo ago
i see
leowest
leowest4mo ago
that's why I suggested u to open a new #help I can only show u what it looks on visual studio
Ip Man
Ip Man4mo ago
i think ill check a video out
leowest
leowest4mo 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
Ip Man
Ip Man4mo ago
alright cheers bro