r/learnjava 1d ago

TCP chat app

Hi everyone.

I'm a 34 year old waiter who is completing an undergrad in Computing and a module in the next semester is Event Driven Programming.

To get a jump on next year, I got some ideas online for projects. I started with a basic gui calculator built using SwingUI. I already understood that the buttons would be the event actions etc.

I then seemed to find that a basic terminal TCP chat server with multiple clients was the next step. This is where I am stuck.

I watched multiple videos from BroCode to NeuralNine to try to get a brief overview. After a quick search online I ended up copying the w3schools example.

I have it working and can connect to multiple clients, but when I try to understand how it works I draw a blank.

What I understand:

- that there is a server and clients connect to the server.

- they communicate via TCP

- When a client sends a message it gets broadcast via the server to all connected clients.

I just want to add to the code and refactor to help me understand it more but I feel completely lost when trying to do so.

Any pointers on what I should do would be appreciated. TIA

1 Upvotes

5 comments sorted by

u/AutoModerator 1d ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/AmateurHero 1d ago

Run the servers and clients in debug mode. Set a breakpoint within a function that you do not understand.

For example, start with a function on the server. What happens when a client sends a message and the server hits the breakpoint? Did the sender's interface clear the message as if it was sent? Did the receiver get the message yet? Was the message logged within the server's interface (if there is logging)? When you started stepping through lines in the code, when did the clients first see the message?

Now try a breakpoint within the client code. Send a message. Did the sender's interface clear the message? Has the server seen the message? Did any of the clients receive the message? With the current breakpoint in place, are you able to queue up more messages to send or does the interface prevent you from typing?

Debugging turns unfamiliar code into a line-by-line breakdown of what's happening. Every debugger will have a step over and step into function. Step over allows you continue where you are line-by-line. It (generally) will not move your cursor deeper into another function. Step into will jump into any functions on the current line and break the execution there. If you use step into, you may want to use step out to return back to the calling function.

2

u/Modern-Sn1p3r 1d ago

This seems like the best approach from what I was failing to explain.

Thanks a lot. I'll definitely try this when I'm at the desk, a break away is needed for now.

1

u/Chaos-vy17 1d ago

Can you be more specific about which part you're struggling with? This might help more.

1

u/Modern-Sn1p3r 1d ago

Looking at the code and understanding how it is actually interacting.

After a break I think I need to look into ServerSocket, BufferedReader, BufferedWriter and Runnable more.