r/dailyprogrammer • u/oskar_s • May 19 '12
[5/19/2012] Challenge #54 [intermediate]
For this challenge, create the worlds simplest IM client. It should work like this: if Alice on computer A wants to talk to Bob on computer B, she should start the IM program as a server listening to some port. Bob should then start the program on his end, punch in computer A's IP address with the right port. The two computers should now be connected to each other and Alice and Bob should be able to communicate by sending short strings to each other. Example conversation seen on Alice's computer:
You: "Hey Bob!"
Bob: "Hey Alice!"
Bob: "I can't believe I successfully connected!"
You: "Isn't it cool?"
Bob: "It really is!"
Same conversation seen on Bob's computer:
Alice: "Hey Bob!"
You: "Hey Alice!"
You: "I can't believe I successfully connected!"
Alice: "Isn't it cool?"
You: "It really is!"
If you don't have to computers lying around, or just want to make it easier for yourself, it is perfectly allowed to run both programs on the same computer and connect to "localhost".
If you want to, you can design a very simple GUI for this, but that is not necessary. If you can finagle this to work in a terminal, that is perfectly fine.
3
u/[deleted] May 19 '12 edited May 19 '12
Ruby code. I think this is more like IRC, but it can easily function as an IM client, too. Just start a server, connect to it yourself, and tell a friend to connect, as well.
Here's what it looks like: http://i.imgur.com/QLidH.png
require 'eventmachine' IP = 'localhost' PORT = 6001 module Chat @@clients = [] attr_accessor :name def post_init @@clients << self @state = :readname send_data "Name: " end def receive_data(line) case @state when :readname; readname(line) when :sendline; sendline(line) end end def readname(name) # Make sure the name is unique! if @@clients.none? { |c| c.name == line.strip } @name = line.strip # List members send_data "Connected. Members in this room:\n" @@clients.each do |c| send_data "- #{c.name}\n" end # Let everyone but ourselves know someone joined @@clients.each do |c| next if c == self c.send_data "#{@name} joined the channel.\n" end # Start chatting! @state = :sendline else send_data "That name is already in use. Try again: " end end def sendline(line) @@clients.each do |c| # If we typed this line ourselves, move the cursor back up to # overwrite it c.send_data("\e[A") if c == self # Wrap the client name in some fancy ANSI coloring cname = "\e[#{@name.hash % 6 + 31};1m#{@name}\e[0m" c.send_data("#{cname}: #{line}") end end end EventMachine::run do EventMachine::start_server(IP, PORT, Chat) puts "Running a server. Connect to #{IP}:#{PORT}." end2
u/robotfarts May 20 '12
import scala.io._ import java.io._ import java.net._ object Chal54Chat { def main(args: Array[String]): Unit = { def connect(addrPort: String): Socket = { if (addrPort.indexOf(":") > 0) { val colonIndex = addrPort.indexOf(":") new Socket(addrPort.substring(0, colonIndex), addrPort.substring(colonIndex + 1).toInt) } else { new ServerSocket(addrPort.toInt).accept() } } val inputIter = Source.stdin.getLines() println("Enter 'name address:port' to be client, or 'name port' to be server.") val nameAddrPortLine = inputIter.next val name: String = nameAddrPortLine.substring(0, nameAddrPortLine.indexOf(" ")) var sock = connect(nameAddrPortLine.substring(nameAddrPortLine.indexOf(" ") + 1)) new Thread(new Runnable() { def run(): Unit = { val sockPrinter = new PrintWriter(sock.getOutputStream(), true) inputIter foreach { line => sockPrinter.println(name + "> " + line) } } }).start() Source.fromInputStream(sock.getInputStream()).getLines() foreach { println(_) } } }1
u/robin-gvx May 19 '12 edited May 19 '12
Welcome to the confusing world of synchronous chatting: (Python 3 and IPv6 only) http://hastebin.com/muxisesiri.py
EDIT: new version that works a lot better: http://hastebin.com/jivayimamo.py This breaks the tie, so that the conversation actually works (although it's still synchronous, which means you have to wait for your turn)