7 Replies - 1130 Views - Last Post: 27 August 2010 - 03:13 AM Rate Topic: -----

#1 aroraaashish   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-August 10

TCP- client and server

Posted 26 August 2010 - 02:01 AM

i want that from server some random strings come to client only i dont want that client send any thing to server.so i have written some code which can be shown as
for server:-
public class TCPServer {

	public static void main(String[] args)throws Exception {
		String str = null;
		char ch;
		ServerSocket server=new ServerSocket(5000);
		System.out.println("waiting for client ...");
		int j=0;
		while (true) {
			
			Socket connected=server.accept();
			System.out.println("data is going to the port of client "+ connected.getPort()+":"+connected.getInetAddress());
			PrintWriter out=new PrintWriter(connected.getOutputStream());
		while (true) {
		for (int i = 0; i <= 5; i++) {
			ch=(char)(Math.abs(Math.random()*100));
	if(str.length()==5){
	System.out.println(str);
	out.println(str);
	str=null;
	j++;	    
	}else{
	if(j>=100){
	str=null;
	out.println(str);
	connected.close();
	break;
	}else{
	str=str+ch;
	}}}}}}}



 // client code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import com.db4odoc.f1.Util;

public class TCPClient {
public static void main(String[] args) throws Exception {
		String str1=null;
		Socket client=new Socket("172.29.26.12",5000);
		BufferedReader fromServer=new BufferedReader(new InputStreamReader(client.getInputStream()));
		while (true) {
			str1=fromServer.readLine();
			if(str1.isEmpty()){
				client.close();
				break;
			}
			else{
		System.out.println("Received :"+ str1);
 			}}}}




the error i am gettin in that is

Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)

kindly help me to sought uot this problem
thanks in advance

This post has been edited by no2pencil: 26 August 2010 - 02:05 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: TCP- client and server

#2 Brewer   User is offline

  • Awesome
  • member icon

Reputation: 182
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: TCP- client and server

Posted 26 August 2010 - 02:59 AM

Are there any firewalls on that could intervene with the connection? This seems to be the most likely cause of a "Connection reset" error.
Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3567
  • View blog
  • Posts: 16,264
  • Joined: 20-September 08

Re: TCP- client and server

Posted 26 August 2010 - 03:03 AM

You're going to run into a lot of difficulty with NPEs in str there. Also your random method will produce garbage. Try the following server:

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

public class TCPServer {
    public static void main(String[] args) throws Exception {
        String str = "";
        char ch;
        ServerSocket server = new ServerSocket(5000);
        System.out.println("waiting for client ...");

        int j = 0;

        while (true) {
            Socket connected = server.accept();
            System.out.println("data is going to the port of client " +
                connected.getPort() + ":" + connected.getInetAddress());

            PrintWriter out = new PrintWriter(connected.getOutputStream());

            while (true) {
                for (int i = 0; i <= 5; i++) {
                    ch = (char) ('A' + (Math.random() * 26));

                    if (str.length() == 5) {
                        System.out.println(str);
                        out.println(str);
                        str = "";
                        j++;
                    } else {
                        if (j >= 100) {
                            out.println(str);
                            str = "";
                            connected.close();

                            break;
                        } else {
                            str += ch;
                        }
                    }
                }
            }
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#4 aroraaashish   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-August 10

Re: TCP- client and server

Posted 26 August 2010 - 05:27 AM

View Post.i7, on 26 August 2010 - 01:59 AM, said:

Are there any firewalls on that could intervene with the connection? This seems to be the most likely cause of a "Connection reset" error.

no i have seen all fire wall issues thanks for replyin me

your answer is really helpful to me and i am thank ful to you that you have paid a attention on it.
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: TCP- client and server

Posted 26 August 2010 - 04:11 PM

How can you expect your Client to receive a datagram (empty or not) from the server until it connects to it ? Your Client never connects to the server
Was This Post Helpful? 0
  • +
  • -

#6 aroraaashish   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-August 10

Re: TCP- client and server

Posted 26 August 2010 - 11:55 PM

View Postpbl, on 26 August 2010 - 03:11 PM, said:

How can you expect your Client to receive a datagram (empty or not) from the server until it connects to it ? Your Client never connects to the server


it is working man i forgot to put true with print writer statement
but thanks for your attention
Was This Post Helpful? 0
  • +
  • -

#7 Mercurial   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: TCP- client and server

Posted 27 August 2010 - 02:56 AM

Hm. Don't you have to flush the stream after writing(PrintWriter) ? Can't remember.
Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3567
  • View blog
  • Posts: 16,264
  • Joined: 20-September 08

Re: TCP- client and server

Posted 27 August 2010 - 03:13 AM

Quote

it is working man i forgot to put true with print writer statement


Your problem is nothing to do with flushing, which occurs automatically when the stream closes
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1