Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dateclient.java:
Java
import java.net.*;
import java.io.*;
import java.util.*;

public class dateclient
{
public static void main(String args[])

{try{
	Socket s=new Socket(InetAddress.getLocalHost(),1000);
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String input;
PrintWriter out=new PrintWriter(s.getOutputStream(),true);

while((input=br.readLine())!=null)
{
	System.out.println(input);
	out.println("Date & Time received....");
	
}
}
catch(Exception e){
System.out.println("The connection to the server has been reset");}
}
}


Dateserver.java
Java
import java.net.*;
import java.io.*;
import java.util.*;

public class dateserver extends Thread
{
public static void main(String args[]) throws Exception
{

ServerSocket ss=new ServerSocket(1000);
Socket s=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
Date d=new Date();
try
{
while(true)
{
out.println("Time at server: "+d.toString());
System.out.println(br.readLine());
sleep(1000);
}
}

catch(IOException e){System.out.println("---Connection to client is reset---");
}
}
}


Hey am a begineer to javaEE.I have a problem receiving an input from server to client once the server gets the client's address.
Am not getting the "Date and time received.." output at the client.

PS:My output is good except that it doesnt print that line alone.Am trying too :D
Posted

1 solution

Okay, I copied your code and ran "as is".

Start the server first and then the client, works fine. I would put into the server an output to indicate that it's waiting for the connection:
Java
System.out.println("Waiting for connection");
Socket s=ss.accept();
System.out.println("Connection received");


Also the date object will remain at the same time as you only instantiate it once. Better would be to move that line inside the while loop
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900