How to send strings over TCP/IP


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,

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

sghosh092199Connect With a Mentor Commented:

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

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

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

public void doInterface()
Frame frame = new Frame();
frame.setLayout(new FlowLayout());


public static void main(String args[])
FormConnect fc = new FormConnect();


Check how it goes.

Wayne BradneyIndependent ConsultantCommented:

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

And for an introduction to GUI programming in Java:

andyknottAuthor Commented:
OK I'll have a go with those examples and let you know how I get on.

The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

andyknottAuthor Commented:
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\ cannot be found on sourcepath by appending \ to each sourcepath entry
"": Error #: 475 : class FormConnect is public; must be declared in a file named at line 6, column 8
"": 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,

andyknottAuthor Commented:
I just cant fix the exception problem - its driving me crazy!
I defined a class called FormConnect. Check that you save it in a file called

The other exception is probably due to a failed connection.
Check the hostname and port numbers are appropriate.
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

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.
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.

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.

------------>  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

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.
Moderator @ Experts Exchange
andyknottAuthor Commented:
Sorry its been so long,

That project got binned and I forgot about EE.

All Courses

From novice to tech pro — start learning today.