473,418 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Why my run method is not working?

108 100+
This program consists of a client1 named client1 and a server named server.My client is able to connect to the server but the server cannot catch what my client is sending as inputs.

Here are the codes
client codes

<code>
import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.io.*;
import java.util.Scanner;


public class client1 {
public static void main(String[] args) {
/** Define a host server */
String host = "localhost";
/** Define a port */
int port = 29999;

StringBuffer instr = new StringBuffer();
String TimeStamp;
System.out.println("SocketClient initialized");
Scanner sc=new Scanner(System.in);

try {

Socket connection = new Socket(host, port);





PrintWriter osw = new PrintWriter(connection.getOutputStream());
TimeStamp = new java.util.Date().toString();

BufferedReader bf=new BufferedReader(new InputStreamReader(connection.getInputStream()));//delete this line

String server_chat;;
String user_chat;
osw.println("sending client input to server");
while ((( sc.hasNextLine())) && (!(user_chat=sc.nextLine()).equals("quit")) && (((server_chat=bf.readLine())!=null) )){
System.out.println("Server says"+user_chat);
osw.println("sending client input to server");
}

}
catch (IOException f) {
System.out.println("IOException: " + f);
}
catch (Exception g) {
System.out.println("Exception: " + g);
}
}
}
</code>



server codes

<code>


//package bdn;
import java.net.*;

import java.io.*;
import java.util.*;

public class server implements Runnable {

private Socket connection;
private String TimeStamp;
private int ID;
public static void main(String[] args) {
int port = 29999;
int count = 0;
try{
ServerSocket socket1 = new ServerSocket(port);
System.out.println("MultipleSocketServer Initialized");
// while (true) {

Socket connection = socket1.accept();

server runnable = new server(connection, ++count);
Thread thread = new Thread(runnable);

thread.start();

// }
}
catch (Exception e) {System.out.println(e.getMessage());}
}
server(Socket s, int i) {
this.connection = s;
this.ID = i;
}
public void run() {
System.out.println("Testing run ok");
try {

String character;



PrintWriter osw = new PrintWriter(connection.getOutputStream());


// StringBuffer process = new StringBuffer();
BufferedReader bf=new BufferedReader(new InputStreamReader(connection.getInputStream()));
while(((character = bf.readLine()) !=null)) {
System.out.println("Server is writing");

osw.print("character");

}



}
catch (Exception e) {
System.out.println(e);
}
finally {
try {
connection.close();
}
catch (IOException e){System.out.println(e);}
}
}
}
</code>
Where have I erred in the codes and how can I improve them?


Thanks in adv
Sep 13 '10 #1
1 1769
Dheeraj Joshi
1,123 Expert 1GB
Does it at least prints
Expand|Select|Wrap|Line Numbers
  1. System.out.println("Testing run ok");
  2.  
this?

I do not think following code is right
Expand|Select|Wrap|Line Numbers
  1. Runnable runnable = new MultipleSocketServer(connection, ++count);
  2.  
It gives error for me. I can not compile.
Is this another class in your project?

Also please wrap the code in code tags.

Regards
Dheeraj Joshi
Sep 13 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
5
by: RickDee | last post by:
Please help, anybody. I am trying to write a program so that it can launch an exe file ( which is also genereated in C# ) and then simulate the button clicking and invoke the methods inside the...
0
by: rao | last post by:
I am trying to get sub nodes for for clicked node using getChildren() method as shown below. I am getting javascript exception error (subtree.null )when nodes are not expanded. The code works only...
2
by: Noozer | last post by:
Hi! Trying to get my head around working with network connectivity in VB.Net. I've gotten a fairly decent control based on the sample in the 101VBSamples available from Microsoft.What I'm having...
9
by: MSDNAndi | last post by:
Hi, I have a set of simple webservices calls that worked fine using .NET Framework 1.0. I am calling a Java/Apache based webservices, the calling side is not able to supply a proper WSDL. ...
0
by: Kevin Blount | last post by:
I found an alternative to string.Replace(...) that will ignore case when looking for things to search, and while it's working a lot better than a standard .Replace(...) it's not working exactly how...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
20
by: Auddog | last post by:
I'm new to working with classes. I'm pretty much a self taught php programmer that uses php mostly for database entry and listings. I would like to expand my talents and start working with...
3
by: =?Utf-8?B?Sm9obkJhdGVz?= | last post by:
I'm trying to (programatically) backup and clear the security event log on the local machine. I can do this manually through the event viewer and I am logged on as an administrator. I can...
7
by: cbmeeks | last post by:
Hope I'm using the right terminology. Anyway, say I have a class like: class Animal { public double GetValues() {......} public void FilterBy(string text); {......}
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.