How to send strings over TCP/IP

Posted on 2001-06-08
Last Modified: 2010-04-16

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,

Question by:andyknott
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 6168012

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

And for an introduction to GUI programming in Java:


Author Comment

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


Accepted Solution

sghosh092199 earned 200 total points
ID: 6168919

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.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

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


Author Comment

ID: 6178901
I just cant fix the exception problem - its driving me crazy!

Expert Comment

ID: 6179052
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.

Expert Comment

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


Expert Comment

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

Author Comment

ID: 6913746
Sorry its been so long,

That project got binned and I forgot about EE.


Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
going to wrong jsp page 2 71
Java: The Public Class Main 4 69
web project error add remove 1 85
Bitwise and to sum elements 2 55
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

710 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