Solved

How to send strings over TCP/IP

Posted on 2001-06-08
9
666 Views
Last Modified: 2010-04-16
Hi,

I'm very new to Java. I usually program in VB 5/6 and have reasonable experience with JavaScript. I have borland Java builder 4.

I need to write a Java application on run on Win NT. The application must send commands, and some data, using TCP/IP to a VB application that is listening on a certain port/sockets. The commands and data should be controlled by buttons on a form. EG press this button to send 'This String' and 'This data'.

Please could you supply me with some sample code to load a form, connect to an IP address and port, then send the commands and data.

Thanks in advance,

Andy
0
Comment
Question by:andyknott
9 Comments
 
LVL 6

Expert Comment

by:wbradney
ID: 6168012
andyknott,

There's a very good socket tutorial with examples on the Sun website:

http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html

And for an introduction to GUI programming in Java:

http://java.sun.com/docs/books/tutorial/uiswing/index.html

Regards,
WMB
0
 

Author Comment

by:andyknott
ID: 6168714
OK I'll have a go with those examples and let you know how I get on.

Thanks
0
 
LVL 3

Accepted Solution

by:
sghosh092199 earned 200 total points
ID: 6168919
Hi!

Here is a sample code that illustrates how an inout string can be sent from a Java application over TCP/IP.

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class FormConnect implements ActionListener
{

Button button = new Button("Send It");
TextField text = new TextField(10);
Socket socket;
PrintWriter out;

public void doConnect() throws Exception
{

// It uses port 9000 on the local host. It should be
//replaced by IP address and port of the receiving VB
//application

socket = new Socket("localhost", 9000);
out = new PrintWriter(new BufferedOutputStream(socket.getOutputStream()));
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == button )
{

out.println(text.getText());
out.flush();
}
}
public void doInterface()
{
Frame frame = new Frame();
frame.setLayout(new FlowLayout());
frame.add(text);
frame.add(button);
button.addActionListener(this);
frame.setSize(200,200);
frame.show();

}

public static void main(String args[])
{
FormConnect fc = new FormConnect();
fc.doConnect();
fc.doInterface();
}



}

Check how it goes.

sghosh
0
 

Author Comment

by:andyknott
ID: 6178229
Hi sghosh,

Thanks for the code. I've got a couple of errors. Probably simple - I find java quite confusing!

Warning #: 908 : check sourcepath; source c:\WINDOWS\jbproject\TCP_IP_Test\src\tcp_ip_test\Untitled1.java cannot be found on sourcepath by appending \Untitled1.java to each sourcepath entry
"Untitled1.java": Error #: 475 : class FormConnect is public; must be declared in a file named FormConnect.java at line 6, column 8
"Untitled1.java": Error #: 360 : unreported exception: java.lang.Exception; must be caught or declared to be thrown at line 48, column 13

What do these errors mean and how do I fix them? Remember I'm using the borland JBuilder 4 environment.

Thanks in advance,

Andy
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:andyknott
ID: 6178901
I just cant fix the exception problem - its driving me crazy!
0
 
LVL 3

Expert Comment

by:sghosh092199
ID: 6179052
I defined a class called FormConnect. Check that you save it in a file called FormConnect.java

The other exception is probably due to a failed connection.
Check the hostname and port numbers are appropriate.
0
 

Expert Comment

by:HuangJo
ID: 6184508
Hi andyknott,

u can actually use servlet to acomplish the tasks u requested. In fact it will be much simpler, as servlet basically handle much of the TCP/IP tasks quite automatically.

Servlet allow both the remote client Java application and Browser based client to communicate with the remote server.

regards Jo

0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6913502
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20131980.html
http://www.experts-exchange.com/questions/Q.20190186.html
http://www.experts-exchange.com/questions/Q.20265539.html
http://www.experts-exchange.com/questions/Q.20276911.html
http://www.experts-exchange.com/questions/Q.20277661.html




PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
0
 

Author Comment

by:andyknott
ID: 6913746
Sorry its been so long,

That project got binned and I forgot about EE.


Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now