• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

how to work with socket

 
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
 
Sheriff
Posts: 21788
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • 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
Posts: 21788
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Rancher
Posts: 43011
76
  • 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.
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!