Skip to content

Latest commit

 

History

History
34 lines (21 loc) · 1.47 KB

02-socket.md

File metadata and controls

34 lines (21 loc) · 1.47 KB

Create the TCP socket (5-10 minutes)

Goals

The goal of this lesson is to create a TCP server on port 6677 that will listen for connections.

Steps

  1. Open your chat.go file and find the main function. Add the following code to main:

  2. 🌟 Create a TCP listener on port 6677. hint: Use the net.Listen function docs here to create a listener that binds to TCP port 6677.

  3. 🌟 Create a new instance of the chatroom using NewChatRoom() and call chatroom.ListenForMessages()

  4. 🌟 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!

  1. 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.

  2. To test your server connection, you can use nc localhost 6677 or telnet localhost 6677 (the server will accept the connection, but it won't respond to messages - we still need to write the code for that!).

Proceed to Lesson 3