Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to work with socket

 
loveAnil Nayak
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:One of the two needs to act as a server.

... for which you'd use a ServerSocket, not a Socket.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic