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

by:Wayne Bradney
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.

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI


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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

617 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