View Single Post
Old 11-21-2004, 02:08 PM   PM User | #8
ghell
Senior Coder

 
Join Date: Apr 2003
Location: England
Posts: 1,192
Thanks: 5
Thanked 13 Times in 13 Posts
ghell is on a distinguished road
i seem to have messed it up even more (port# censored but its there really )

server:
Code:
import java.lang.*;
import java.io.*;
import java.net.*;

public class Server
{
	public static void main(String args[])
	{
		String data;
		int intPort = ####;
		
		ServerSocket		objServerSocket;
		Socket			objSocket;
		InputStreamReader	objInStreamReader;
		BufferedReader		objInBuffer;
		PrintWriter		objOutStream;
		
		try
		{
			//---Start listening on port---
			objServerSocket = new ServerSocket(intPort);
			
			objSocket = objServerSocket.accept();
			System.out.println("Client has connected to server!");
			
			//---Set up streams---
			objInStreamReader = new InputStreamReader(objSocket.getInputStream());
			objInBuffer = new BufferedReader(objInStreamReader);
			
			objOutStream = new PrintWriter(objSocket.getOutputStream(), true);
			
			//---Receive Data---
			while (objInBuffer.ready())
			{
				data = objInBuffer.readLine();
				
				//---Echo clients data back---
				System.out.println("Echoing string: '" + data + "'");
				objOutStream.print(data);
			}
			
			//---Clean Up---
			objOutStream.close();
			objSocket.close();
			objServerSocket.close();
		}
		catch(BindException be)
		{
			System.out.println("BindException: \n\t" + "Port " + intPort + " already in use");
		}
		catch(Exception e)
		{
			System.out.println("Exception: \n\t" + e);
		}
	}
}
outputs:
Quote:
---BUILDING---
---RUNNING---
Client has connected to server!
Press any key to continue . . .
client:
Code:
import java.lang.*;
import java.io.*;
import java.net.*;

public class Client
{
	public static void main(String args[])
	{
		String data = "DataFromClient";
		
		Socket			objSocket;
		InputStreamReader	objInStreamReader;
		BufferedReader		objInBuffer;
		PrintWriter		objOutStream;
		
		//---Get Address---
		String strFullAddress = args[0];	//"localhost:####";
		
		String[] arrFullAddress = strFullAddress.split(":");
		String strAddress = arrFullAddress[0];
		int intPort = Integer.parseInt( arrFullAddress[1] );
		
		//System.out.println("Address:\t" + strAddress + "\nPort:\t\t" + intPort);
		//-----------------
		
		//String strAddress = "localhost";
		//int intPort = ####;
		
		try
		{
			System.out.println("Connecting to: '" + strFullAddress + "'");
			objSocket = new Socket(strAddress, intPort);
			
			objInStreamReader = new InputStreamReader(objSocket.getInputStream());
			objInBuffer = new BufferedReader(objInStreamReader);
			
			objOutStream = new PrintWriter(objSocket.getOutputStream(), true);
			
			//---Send Line---
			System.out.println("Sending string: '" + data + "'");
			objOutStream.print(data);
			
			while (objInBuffer.ready())
			{
				//---Receive and Print Line---
				System.out.println("Receiving String: '" + objInBuffer.readLine() + "'");
			}
			
			//---Clean Up---
			objOutStream.close();
			objInBuffer.close();
			objSocket.close();
		}
		catch(ConnectException ce)
		{
			System.out.println("Could not connect to server");
		}
		catch(Exception e)
		{
			System.out.println("Exception: \n\t" + e);
		}
	}
}
outputs:
Quote:
---BUILDING---
---RUNNING---
Connecting to: 'localhost:####'
Sending string: 'DataFromClient'
Press any key to continue . . .
when i had server sending data and client receiving it worked, but when i set client to send and then server to receive and echo back, it stopped, i dont know which side its stopped on
__________________
My tech/code blog
ghell is offline   Reply With Quote