The Artima Developer Community
Sponsored Link

Java Answers Forum
Problem on Socket Program

2 replies on 1 page. Most recent reply: May 20, 2005 7:03 PM by KL Lam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
KL Lam

Posts: 2
Nickname: lam
Registered: May, 2005

Problem on Socket Program Posted: May 12, 2005 12:34 AM
Reply to this message Reply
Advertisement
Hi all, please me to solve the problem.

I created a socket program TCPClient and communicate to Server. The program is workable when run on the 1st time. But it will hang up on the code 'while ((result=in.readLine()) != null)'

Here is my program.

import java.net.*;
import java.io.*;

public class TCPClient
{
String inputData, sendData;
String tmpCode, ctlCode;
int intCh;
char ch;
//private static String result;
StringBuffer chkChr;

public void TCPsend(String passData)
{
try
{
int serverPort = 26001;
String hostName = "172.19.1.111";

inputData = passData;

// Connet to the Server
Socket s = new Socket(hostName, serverPort);
DataOutputStream out = new DataOutputStream( s.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

// System.out.println("Inet Address " + s.getInetAddress());

// add device control code (Hex 13 or ASCII 19)at the end of record
tmpCode = " ";
intCh = 19;
ch = (char) intCh;
StringBuffer word = new StringBuffer(tmpCode);
word.setCharAt(0, ch);
ctlCode = word.toString();

// sendData = inputData + ctlCode;

sendData = inputData;
System.out.println("send record: " + sendData);
out.writeBytes(sendData);

sendData = ctlCode;
System.out.println("send record: " + sendData);
out.writeBytes(sendData);
out.flush();
try
{
String result;
while ((result=in.readLine()) != null)
{
System.out.println(result);
if (result.equals(ctlCode))
break;
}
}
catch (IOException e) { System.out.println("readLIne failed : " + e.getMessage()); }

System.out.println("while routine completed ");



// clear routine
out.close();
in.close();
s.close();

System.out.println("Client Socket closed successed");

}
catch (UnknownHostException e)
{
System.out.println("Sock:"+e.getMessage());
}
catch (EOFException e)
{
System.out.println("EOF:"+e.getMessage());
}
catch (IOException e)
{
System.out.println("Err IO:"+ e.getMessage()) ;
}



} // TCPsend
} // End class


Haik Hovsepian

Posts: 6
Nickname: haik
Registered: Oct, 2004

Re: Problem on Socket Program Posted: May 20, 2005 12:27 AM
Reply to this message Reply
Hello.
The programm is hanging ou because the method in.readLine()
is wating for a text,if text comes it continues.
You must use this code


BufferedReader ois = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));

while (true) {

if (ois.ready()) {

inputLine = ois.readLine();
System.out.println(inputLine);

}


}

KL Lam

Posts: 2
Nickname: lam
Registered: May, 2005

Re: Problem on Socket Program Posted: May 20, 2005 7:03 PM
Reply to this message Reply
Thanks Haik,

One more question. How can I terminate the ois.readLine()when each records have a "^S" (ASCII code 19) character to indicate the end of the record?

Flat View: This topic has 2 replies on 1 page
Topic: write method gets  blocked while communicating to COM port Previous Topic   Next Topic Topic: help on a palindrome program, please?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use