CodeGym
Promotion
CodeGym University
Learning
Courses
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
CodeGym/Help with Java Tasks/question
Wei Cui
Level 39
Newark
  • 4/15/20
  • 1178views
  • 2comments

question

Question about the task Chat (part 5)
Java Multithreading,  Level 6,  Lesson 15
Under discussion

The client and server will communicate through a socket connection.
One side will write data to the socket, while the other will read. They interact by exchanging Messages.
The Connection class will wrap the java.net.Socket class,
which needs to be able to serialize and deserialize Message objects to/from the socket.
The methods of this class should be callable from different threads.

Add the following to the Connection class:

1) Final fields:
a) Socket socket
b) ObjectOutputStream out
c) ObjectInputStream in

2) A constructor that has a Socket parameter and initializes the object's fields.
To initialize the in and out fields, use the socket's corresponding threads.
The constructor may throw an IOException.
You need to create an ObjectOutputStream object before creating an ObjectInputStream object.
Otherwise, threads trying to establish a connection through the Connection class may block each other.
You can read more about this in the documentation for the ObjectInputStream class.

3) A void send(Message message) throws IOException method.
It should write (serialize) the message to the ObjectOutputStream.
This method will be called from multiple threads.
Be sure that only one thread can write to the ObjectOutputStream object at a time.
Other threads must wait their turn. However, other methods of the Connection class should not be blocked.

4) A Message receive() throws IOException, ClassNotFoundException method. It should read
(deserialize) data from the ObjectInputStream.
Make it so a read operation can't be performed simultaneously by multiple threads,
but be sure not to block calls to other methods of the Connection class.

5) A SocketAddress getRemoteSocketAddress() method, which returns the remote address of the socket connection.

6) A void close() throws IOException method, which should close all the resources used by the object.

The Connection class should support the Closeable interface.

Requirements:
  • The Connection class should support the Closeable interface.
  • The Connection class must have a private final Socket socket field.
  • The Connection class must have a private final ObjectOutputStream out field.
  • The Connection class must have a private final ObjectInputStream in field.
  • The Connection class must have a constructor with a single Socket parameter that initializes the object's fields according to the task conditions.
  • The Connection class must correctly implement the send() method with one Message parameter.
  • The Connection class must correctly implement the receive() method without parameters.
  • The Connection class's getRemoteSocketAddress() method must return the remote address of the socket connection.
  • The Connection class's close() method should close the input stream, output stream, and socket connection.
package com.codegym.task.task30.task3008; public class Server { public static void main(String[] args) {} }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Norbert Backend Developer
9 November 2020, 15:37
correct sent() to send()
0
Oskar
Level 31 , Poznań, Polska
22 April 2020, 15:09
package com.codegym.task.task30.task3008;

import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketAddress;

public class Connection implements Closeable {
    private final Socket socket;
    private final ObjectOutputStream out;
    private final ObjectInputStream in;

    public Connection(Socket socket) throws IOException {
        this.socket = socket;
        this.out = new ObjectOutputStream(socket.getOutputStream());
        this.in = new ObjectInputStream(socket.getInputStream());
    }

    public  void send(Message message) throws IOException {
        synchronized (out) {
            out.writeObject(message);
        }
    }

    public Message receive() throws IOException, ClassNotFoundException {
        synchronized (in) {
            return (Message) in.readObject();
        }
    }

    public SocketAddress getRemoteSocketAddress() {
        return socket.getRemoteSocketAddress();
    }

    public void close() throws IOException {
        in.close();
        out.close();
        socket.close();
    }
}
+2
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning, and a Java developer’s career.
Follow us
Interface language
English
Deutsch Español हिन्दी Français Português Polski বাংলা 简体中文 मराठी தமிழ் Italiano Bahasa Indonesia 繁體中文 Nederlands 日本語 한국어 Bulgarian Danish Hungarian Basa Jawa Malay Norwegian Romanian Swedish Telugu Thai Українська Filipino Turkish Azərbaycan Русский Vietnamese
Programmers Are Made, Not Born © 2026 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2026 CodeGym