aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes how to work with socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "how to work with socket" Watch "how to work with socket" New topic
Author

how to work with socket

loveAnil Nayak
Greenhorn

Joined: Dec 29, 2009
Posts: 3
actually i have two GUI programs

1.Client1
2.Client2

Client1 will send data Client2 will receive and vice versa.

but when one program is using socket like:1234 then for other its showing No I/O another problem is

if one sends any data to other programs buffer and the other program will automatically receive the data and display it in the textarea without any actionListener class; just like in chating program are doing

tell me how to do it
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

1) To be able to communicate between two Java applications one of them needs to have a ServerSocket on which you listen. The other uses a Socket to connect to that ServerSocket. Check out http://java.sun.com/docs/books/tutorial/networking/sockets/

2) Upon receiving you need to notify the user interface. Usually you do this using EventQueue.invokeLater* (read http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html for why). If you use JTextArea.append though you don't need that, as that method is one of the very very few thread safe methods in Swing.


* Some people use SwingUtilities.invokeLater. This method was initially created before EventQueue.invokeLater existed, and now merely delegates to that method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi loveAnil,

welcome to JavaRanch

I'm not really sure what's your question. I think your problem is basically how to create a client-server-application which is capable to send and receive data on both ends of the connection (i.e. server and client), right?

Marco
loveAnil Nayak
Greenhorn

Joined: Dec 29, 2009
Posts: 3

actually i have two GUI programs

1.Client1
2.Client2


Client1 will send data Client2 will receive and vice versa.


i have used socket but when the socket object is created by
Socket client = new Socket(InetAddress.getLocalHost(),1581);

Socket server = new Socket(InetAddress.getLocalHost(),1581);

what happened that on program is running and other get closed automatically because one socket is not shared by two program simultaniously

how to use the socket or any other class so that both side communication will be possible

and another problem is

if client1 program is sending data to client2 program with out interfering to that program client1 how to display data in GUI of client1(textarea)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

One of the two needs to act as a server. Which one you should decide. If you don't add this server part then there simply is nothing listening on port 1581 and the connection will fail.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42609
    
  65
Rob Prime wrote:One of the two needs to act as a server.

... for which you'd use a ServerSocket, not a Socket.


Ping & DNS - my free Android networking tools app
 
 
subject: how to work with socket