Results 1 to 4 of 4
- 05-08-2009, 12:16 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
[SOLVED] Can anyone tell me what Im doing wrong with this socket
Hi
I am writing a client/server application. I am trying to send an integer from the server to the client, but when displaying the integer back from the client, I keep getting the value 0.
this is the server:-
import java.io.*;
import java.net.*;
import java.nio.*;
import java.util.Random;
import java.util.Scanner;
import java.util.*;
public class TCPserver
{
public static void main( String args[] )
{
// Declare a server socket and a client socket for the server
ServerSocket serviceSocket = null;
Socket clientSocket = null;
//Scanner class
Scanner myScanner = new Scanner(System.in);
//variable n to hold matrix dimensions
System.out.println("Please enter value of n\n");
int n = myScanner.nextInt();
//variables i,j,k for Matrix Multiplication
int i;
int j;
int k;
int m;
//variables for measuring efficiency
long start, stop;
double time;
//Call Random to make values for Matrices
Random r = new Random();
//p to set limit on random number generation
int p;
p = 100;
m = n/2;
//To parallize solution divide into two
//create Matrices A[][], B[][], C[][] and D[][]
//Matrix D[][] to hold value returned from client
int [][]A = new int[m][n];
int [][]B = new int[n][n];
int [][]C = new int[n][n];
int [][]D;
int [][]AA = new int[m][n];
int line;
// Declare an input stream and String to store message from client
BufferedReader is;
//Object input
InputStream in;
ObjectInputStream recieve;
Object recieved;
//OutputStream
DataOutputStream os;
OutputStream out;
//create values of A[][] and B[][] matrices
for(i = 0;i<m;i++)
{
for(j =0; j<n;j++)
{
A[i][j] = r.nextInt(p);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
AA[i][j] = r.nextInt(p);
}
}
for(i =0;i<n;i++)
{
for(j =0;j<n;j++)
{
B[i][j] = r.nextInt(p);
}
}
//perform matrix multiplication with half of A[][], complete B[][]
/*for (i=0;i<m;i++)
for (j=0;j<n;j++)
for (k=0;k<n;k++)
C[i][j] = C[i][j] + A[i][k] * B[k][j];*/
//open socket on port
try
{
serviceSocket = new ServerSocket(1025);
}
catch (IOException e)
{
System.out.println(e);
}
// ServerCoket for Connection
try
{
clientSocket = serviceSocket.accept();
//normal input/output Stream reader
is = new BufferedReader( new InputStreamReader(
clientSocket.getInputStream()));
os = new DataOutputStream(clientSocket.getOutputStream());
//Object output/input Stream
out = clientSocket.getOutputStream();
ObjectOutputStream send = new ObjectOutputStream(out);
in = clientSocket.getInputStream();
recieve = new ObjectInputStream(in);
os.writeInt(m);
//send Matrices A[][] and B[][]
//start counter
start = System.currentTimeMillis();
send.writeObject(AA);
send.flush();
send.writeObject(B);
send.flush();
//recieve C from client
recieved = recieve.readObject();
//add C from client as D[][] and C[][] from server together
for (i=0;i<m;i++)
for (j=0;j<n;j++)
for (k=0;k<n;k++)
C[i][j] = C[i][j] + A[i][k] * B[k][j];
D = (int[][])recieved;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
C[i][j] = C[i][j] + D[i][j];
}
}
stop = System.currentTimeMillis();
//stop
}
catch (IOException e)
{
System.out.println(e);
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
my problem lies on the line within the try catch statement
os.writeInt(m); where the value of m is half of n, n being an Integer that I enter at the Keyboard.
This is my client program:-
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.nio.*;
import java.util.*;
public class TCPclient
{
public static void main(String[] args)
{
// Declare client socket
Socket clientSocket = null;
Scanner myScanner = new Scanner(System.in);
// Declare output stream and string to send to server
DataOutputStream os = null;
int request;
//variables for matric multiplication
int i;
int j;
int k;
//initial value of n
int n = 0;
//value of n from server
int nn = 0;
//matrices A[][],B[][], C[][]
int A[][] = new int[n][n];
int B[][] = new int[n][n];
int C[][] = new int[n][n];
// Declare input stream from server and string to store input received from server
BufferedReader is = null;
int responseLine;
String response;
//Object input/output Streams
InputStream in;
OutputStream out;
ObjectInputStream recieve;
//Variables to store A[][] and B[][] objects from server
Object recieved;
Object recievedB;
try
{
//initilise Socket, input/output stream to connect to server
clientSocket = new Socket("localhost", 1025);
os = new DataOutputStream(clientSocket.getOutputStream());
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: hostname");
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: hostname");
}
// Write data to the socket
if (clientSocket != null && os != null && is != null)
{
try
{
request = 0;
//initialise Object input.output streams
in = clientSocket.getInputStream();
recieve = new ObjectInputStream(in);
out = clientSocket.getOutputStream();
ObjectOutputStream send = new ObjectOutputStream(out);
while(request != 1)
{
nn = is.read();
System.out.println(nn);
request++;
//get A[][] & B[][] from server
recieved = recieve.readObject();
recievedB = recieve.readObject();
System.out.println(recieved);
//A[][] & B[][] == server A[][] & B[][]
A = (int[][])recieved;
B = (int[][])recievedB;
//Perform matrix multiplication on A[][] and B[][]
//to get C[][]
for (i=0;i<nn;i++)
for (j=0;j<nn;j++)
for (k=0;k<nn;k++)
C[i][j] = C[i][j] + A[i][k] * B[k][j];
//send results of C[i][j] back to server as Object
send.writeObject(C);
//flush the stream
send.flush();
}//end while
// Close input/output streams and socket
}//end try
catch (UnknownHostException e)
{
System.err.println("Trying to connect to unknown host: " + e);
}
catch (IOException e)
{
System.err.println("IOException: " + e);
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
}//end if
}
}
I use the lines:-
nn = is.read();
System.out.println(nn);
in order to attach the incoming variable to variable nn in the client. I then display the value of nn.
However all I get is:-
0
[[I@190d11
----jGRASP: operation complete.
where the 0 is meant to be the value of 0. The other data is an array, that works fine.
Any help appreciated
- 05-08-2009, 01:15 AM #2
Code tags would be nice. You seem to have streams flying about all over the place. A socket should have one input and one output yet I count 6 stream/reader declarations in both the client and server.
If you write with a DataOutputStream you have to read with a DataInputStream. Character streams and Object streams should likewise match up.
You should also check what you're writing to a stream as well as what comes out the other end, so you know where the problem is.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-08-2009, 01:28 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
Thank you for the advice, that is extremely helpful
- 05-08-2009, 01:48 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
Similar Threads
-
append response to the request from Socket and write to another socket
By vaibhav_singh_vs@yahoo.co in forum NetworkingReplies: 3Last Post: 04-17-2009, 07:02 PM -
help about Socket
By fahien_akim in forum New To JavaReplies: 0Last Post: 04-16-2009, 10:41 AM -
Socket
By rob in forum New To JavaReplies: 1Last Post: 03-19-2009, 02:24 PM -
Socket ?
By barusk in forum NetworkingReplies: 0Last Post: 03-05-2009, 04:51 PM -
Socket
By vortex in forum New To JavaReplies: 2Last Post: 05-25-2008, 06:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks