java advice needed

Questions about programming languages and debugging
Post Reply
User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

java advice needed

Post by l0ngb1t »

i am creating a small IM application, it aims to allow 2 user on the LAN to chat.

but i am having an issue with concept, so i need some help to clear it up.
so far i think that 2 socket are needed 'cause the users must be able to send and receive messages simultaneously....

so threads are involved, should i create 2 threads, 1 responsible to send the user message, and the other to receive messages, each one use 1 socket different then the other thread.
+ if 2 threads are running should yield() between them or leave them to CPU time sharing to decide ?

am i thinking right ?
if no, can someone put me on the right direction.

P.S no server is involved, just p2p application.
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: java advice needed

Post by Gogeta70 »

I'm currently working on a long term project, and one of the features is voip chat. I made a small test application that just connects directly to your partner (p2p) and it has them connect to you and then you can chat. My method was to process incoming data in one thread and outgoing data in another thread. The method worked really well, so i think you're on the right track.

One thing, though. If you share any variables between threads, you need to be wary of race conditions. If you don't know what they are, just google "multithreading race conditions". The gist of it is that while one thread is writing data to a variable, another thread could be reading that same variable at the same time, and thus get corrupted data.

Using C++ and the windows api, there are a few different ways of making your data thread-safe: mutexes, semaphores, critical sections, and events, which you can read more about here.

If you would like to see the source code to my test voip app, feel free to ask, but it's in C++.

Good luck ^_^
¯\_(ツ)_/¯ It works on my machine...

User avatar
Lundis
Distorter of Reality
Distorter of Reality
Posts: 543
Joined: 22 Aug 2008, 16:00
15
Location: Deadlock of Awesome
Contact:

Re: java advice needed

Post by Lundis »

I wrote a chat client in java a few years back. You can send and recieve on the same socket. The socket is just a bunch of information where your data should be sent / recieved from, so there's no collision if you do both at the same time. Also note that for most of the time no data will be sent between them, so a collision is unlikely.

About threads, assuming you're gonna have a GUI, you could create one thread for reading the socket and running whatever code you need to run (like showing a message etc) and let the main thread be used by swing. If it's a console app, you just need one, since it's be easy to read user input and send messages and then read the socket in a loop.

Actually, now that I think about it, perhaps you don't need another thread for swing, it might create its own when you create a window. Note that you also need a mechanism to decide who's gonna listen and who's gonna connect. On my client I spammed the local network with udp packets to find the server (that responded to the call), I could dig up the code for that if you wanna check it out.

User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Re: java advice needed

Post by l0ngb1t »

@ lundis
collision is not what i am worried about, it's the ability to send and receive messages at the same time, 'cause one socket can't do both at he same exact moment...
and i'll be glad if you can share the code.

@gog thank you. and yes i don't mind id the code is weritten in C++, my first language was C++, been a while since i wrote in it :( but i still like it... so it'll be very helpful to read your code.

+ i was thinking that i can do it without the need for thread :/
can i just use one socket that listen all the time to incoming data and it switch to sending data only the moment that i need to, than it go back to listening ?
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: java advice needed

Post by Gogeta70 »

I don't think you can switch between the sockets only when needed. Gotta go to work now though, will talk more on this later. Here's the code: http://code.suck-o.com/42359" onclick="window.open(this.href);return false;

Enjoy ^_^
¯\_(ツ)_/¯ It works on my machine...

User avatar
Lundis
Distorter of Reality
Distorter of Reality
Posts: 543
Joined: 22 Aug 2008, 16:00
15
Location: Deadlock of Awesome
Contact:

Re: java advice needed

Post by Lundis »

It's 3 years since I coded this shit, it was my first app... The variable/function names aren't very good and the code's a mess in general :D

Looks like I used one thread for each socket.

About sending and receiving at the same time, have you verified that your problem exists? Who told you that you can't send and receive at the same time? ;)
Attachments
lpcp.zip
(26.29 KiB) Downloaded 74 times

User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Re: java advice needed

Post by l0ngb1t »

my point lundis is that the moment the socket is sending.. it cannot receive.. 'cause the method send and receive are synchronized...
i know that it is not likely that this scenario will accrue.. but just in case
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

User avatar
Lundis
Distorter of Reality
Distorter of Reality
Posts: 543
Joined: 22 Aug 2008, 16:00
15
Location: Deadlock of Awesome
Contact:

Re: java advice needed

Post by Lundis »

The data to be sent and received is buffered by the OS, you don't have to read it from the socket right away. Simplified, the socket is just a mostly-read-only description of where the packet should be sent and what packets received by the NIC should be sent to the application. It would make no sense if you can't read and send at the same time ;)

User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Re: java advice needed

Post by l0ngb1t »

Thank you lundis.
i think more reading is required to fully understand this topic. my current info aren't so good i guess, so google here we come :lol:
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: java advice needed

Post by maboroshi »

I wrote a really simple p2p chat app in Python :-) if it helps at all

Code: Select all

from Tkinter import *
import socket
from threading import *

def server():
    port = 9000
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((socket.gethostbyname(socket.gethostname()), int(port)))
    #print socket.gethostbyname(socket.gethostname())
    while True:
        data, addr = s.recvfrom(1024)
        textbox.insert(END, str(addr) + ": " + str(data) + "\n")


def client():
    port = 9000
    host = str(entryhost.get())
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.sendto(str(textboxsend.get()), (host, int(port)))
    textbox.insert(END, str(textboxsend.get()) + "\n")
    textboxsend.delete(0, END)

root = Tk()
root.title("Simple P2P Chat")
root.wm_iconbitmap("shinobi.ico")
mythread = Thread(target=server)
mythread.start()

frame = Frame(root)
entryhost = Entry(frame)
entryhost.pack(side=LEFT, fill=X, expand=True)
label = Label(frame, text="Enter IP or Domain")
label.pack(side=LEFT)
frame.pack(fill=X)

frame = Frame(root)
textbox = Text(frame)
textbox.pack(fill=BOTH, expand=True)
frame.pack(fill=BOTH, expand=True)

frame = Frame(root)
textboxsend =  Entry(frame)
textboxsend.pack(side=LEFT, fill=X, expand=True)
label = Label(frame, text="Enter Your Message")
label.pack(side=LEFT)
frame.pack(fill=X)

frame = Frame(root)
button = Button(frame, text="Send Message", command=client)
button.pack(fill=X, expand=True)
frame.pack(fill=X)

root.mainloop()

Post Reply