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

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.

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.


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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 96
Java Jpanels and Jframe 8 33
custom annotations 9 38
Java exception bubble up 2 18
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

770 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