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