The goal of this lesson is to create a TCP server on port 6677 that will listen for connections.
-
Open your
chat.go
file and find themain
function. Add the following code tomain
: -
🌟 Create a TCP listener on port
6677
. hint: Use thenet.Listen
function docs here to create a listener that binds to TCP port6677
. -
🌟 Create a new instance of the chatroom using
NewChatRoom()
and callchatroom.ListenForMessages()
-
🌟 Write some code that will loop listen for accepted connections on port 6677, and print out the remote address of the connection using
log
.
hint: Use the listener.Accept()
function to accept connections and print
the remote address when the connection is joined. You
can use the RemoteAddr()
function on net.Conn
to do this. Docs for the net package and example are here
Stuck on any of the steps above? Ask your TA, or see the solution!
-
Run the server using
go run chat.go
. It should wait after the "Chat server starting!" message. You can stop the server using CTRL-C. -
To test your server connection, you can use
nc localhost 6677
ortelnet localhost 6677
(the server will accept the connection, but it won't respond to messages - we still need to write the code for that!).