Il semble que l'obstination soit la premiere vertu des programmeurs, et cela tombe bien, parce que je suis du genre tetu: le probleme est resolu, de maniere elegante et optimale, c'est une autre question, mais l'echange se fait. A titre informatif, je joins les 2 codes. Si quelqu'un a des critiques a emettre, elles sont les bienvenues. En attendant, cela peut toujours servir a quelqu'un.
boolean VERBOSE = true; // turn on/off debugging output static int BUFFSIZE = 128000; // memory for buffer static int DOUBLE_SIZE = 8; byte buff[]; byte data[]; int port; ServerSocket server; Socket sock; BufferedInputStream input; BufferedOutputStream output;
public Server(int p) throws IOException { port = p;
try { server = new ServerSocket(port, 100); } catch ( IOException e ) { e.printStackTrace(); }
// amortize the buffer allocation buff = new byte[BUFFSIZE]; }
// wait for somebody to connect on our socket public void connect() throws IOException { sock = server.accept();
if (VERBOSE) System.out.println("Server: opening socket to " + sock.getInetAddress().getHostName() + " on port " + port);
input = new BufferedInputStream(sock.getInputStream(), BUFFSIZE); output = new BufferedOutputStream(sock.getOutputStream(),BUFFSIZE);
// now wait for test byte ack[] = new byte[1]; if (VERBOSE) System.out.println("Waiting for test A..." ); input.read(ack); if (VERBOSE) System.out.println("Test A recieved: "+ack[0]); }
// recieve a test from the client public void recv_testB() throws IOException { byte ack[] = new byte[1]; if (VERBOSE) System.out.println("Waiting for test B..." ); input.read(ack); if (VERBOSE) System.out.println("Test B recieved: "+ack[0]); }
// send a test to the client public void send_testC() throws IOException { int ack = 2;
if (VERBOSE) System.out.println("Sending test C..." ); output.write(ack); output.flush(); }
// shutdown the socket public void closesocket() throws IOException { output.close(); input.close(); sock.close(); if (VERBOSE) System.out.println("Server: closing socket" ); }
public void send_doubles(double vals[], int len) throws IOException { if (VERBOSE) System.out.print("Server: sending " + len +" doubles: " );
/* convert our array of floats into an array of bytes */ ByteArrayOutputStream bytestream; bytestream = new ByteArrayOutputStream(len*DOUBLE_SIZE); DataOutputStream out; out = new DataOutputStream(bytestream);
for (int i=0; i<len; i++) { out.writeDouble(vals[i]); if (VERBOSE) System.out.print(vals[i]+" " ); } byte byte_array[] = bytestream.toByteArray();
byte flip_array[] = new byte[bytestream.size()]; int i, j;
public int recv_doubles(double val[], int maxlen) throws IOException { int i; int numbytes; int totalbytes = 0; byte data[] = new byte[maxlen*DOUBLE_SIZE]; /* for performance, we need to receive data as an array of bytes and then convert to an array of doubles */ if (maxlen*8>BUFFSIZE) System.out.println("Sending more doubles then will fit in buffer!" );
while (totalbytes < maxlen*DOUBLE_SIZE) { numbytes = input.read(data); // copy the bytes into the result buffer for (i=totalbytes; i<totalbytes+numbytes; i++) buff[i] = data[i-totalbytes]; totalbytes += numbytes; }
// now we must convert the array of bytes to an array of doubles ByteArrayInputStream bytestream_rev; DataInputStream instream_rev; byte flip_array[] = new byte[totalbytes]; int j;
for (i = 0; i < totalbytes / DOUBLE_SIZE; i++) { for (j = 0; j < DOUBLE_SIZE; j++) { flip_array[(i+1)*DOUBLE_SIZE - j - 1] = buff[i * DOUBLE_SIZE + j]; }
}
bytestream_rev = new ByteArrayInputStream(flip_array); instream_rev = new DataInputStream(bytestream_rev);
for (i=0; i<maxlen; i++) val[i] = instream_rev.readDouble();
if (VERBOSE) { System.out.print("Server: received " + maxlen + " doubles: " ); for (i=0; i<maxlen; i++) System.out.print(val[i]+" " ); System.out.println("" ); } return maxlen; }
public static void main(String args[]) throws IOException, InterruptedException { Server s = new Server(5010); s.connect(); s.recv_testB(); s.send_testC(); double val[] = new double[3]; s.recv_doubles(val, 3); s.send_doubles(val, 3); s.closesocket(); } }
********************* ***** client c ******** *********************
#define BUFFSIZE 64000 #define VERBOSE 1 // turn on or off debugging output
typedef struct{ int port; // the port I'm listening on int new_fd; // new connection on new_fd struct sockaddr_in serv_addr; // connector's address information double buffer[BUFFSIZE]; // memory for buffer double buffer2[BUFFSIZE]; // memory for temporary buffer } Client;
// create a new client Client newClient (p, test) int p, test; { Client c; c.port = p;
if (VERBOSE) printf("Client: opening socket on port = %d \n", c.port); if ((c.new_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) perror("socket" );
// send out test byte char temp[1]; temp[0] = '.'; if (VERBOSE) printf("Sending test A: %d\n", temp[0]); send(c.new_fd, temp, 1, 0);
return c; };
// send a test to the server void send_testB(c) Client c; { char temp[1]; temp[0] = 1; if (VERBOSE) printf("Sending test B: %d\n", temp[0]); send(c.new_fd, temp, 1, 0); };
// recieve a test from the server void recv_testC(c) Client c; { char temp[1]; int total = 0;
if (VERBOSE) printf("Waiting for test C...\n" ); while (total < 1){ total += recv(c.new_fd, temp, 1, 0); } if (VERBOSE) printf("Test C recieved: %d\n",temp[0]); };
// shut down the socket void closesocket(c) Client c; { if (VERBOSE) printf("Client: closing socket\n" ); close(c.new_fd); };
// send some doubles over the wire void send_doubles(c, val, len) Client c; double *val; int len; { double *buff; char *ptr, *valptr; int i,j;
if (VERBOSE) { printf("Client: sending %d doubles: ", len); for (i=0; i<len; i++) printf("%0.3f ", val[i]); printf("\n" ); } };
// receive some doubles, returns num of doubles received int recv_doubles(c, val, maxlen) Client c; double *val; int maxlen; { int numbytes = 0; int i, j; char *temp; char *result; int end = 0; int total_bytes = 0; temp = (char *) c.buffer; result = (char *) c.buffer2; j = 0;
// we receiving the incoming doubles one byte at a time // oh cross language sockets are so much fun...
while (!end) { if ((numbytes=recv(c.new_fd, temp, BUFFSIZE, 0))==-1) perror("recv" ); for (i=0; i<numbytes; i++) { result[j] = temp[i]; j++; } total_bytes = total_bytes + numbytes; if (total_bytes==maxlen) end = 1; }
// now we need to put the array of bytes into the array of floats char *ptr; int num = j/sizeof(double); ptr = (char *) val;
for (i = 0; i < j; i++) ptr[i] = result[i];
if (VERBOSE) { printf("Client: received %d doubles: ", num); for (i=0; i<num; i++) printf("%e ", val[i]); printf("\n" ); } return num; };
Marsh Posté le 11-06-2008 à 19:04:51
Bonjour a tous,
Il semble que l'obstination soit la premiere vertu des programmeurs, et cela tombe bien, parce que je suis du genre tetu: le probleme est resolu, de maniere elegante et optimale, c'est une autre question, mais l'echange se fait.
A titre informatif, je joins les 2 codes. Si quelqu'un a des critiques a emettre, elles sont les bienvenues. En attendant, cela peut toujours servir a quelqu'un.
*************************
***** serveur java *******
*************************
import java.io.*;
import java.net.*;
public class Server {
boolean VERBOSE = true; // turn on/off debugging output
static int BUFFSIZE = 128000; // memory for buffer
static int DOUBLE_SIZE = 8;
byte buff[];
byte data[];
int port;
ServerSocket server;
Socket sock;
BufferedInputStream input;
BufferedOutputStream output;
public Server(int p) throws IOException
{
port = p;
try { server = new ServerSocket(port, 100); }
catch ( IOException e ) { e.printStackTrace(); }
// amortize the buffer allocation
buff = new byte[BUFFSIZE];
}
// wait for somebody to connect on our socket
public void connect() throws IOException
{
sock = server.accept();
if (VERBOSE) System.out.println("Server: opening socket to " + sock.getInetAddress().getHostName() + " on port " + port);
input = new BufferedInputStream(sock.getInputStream(), BUFFSIZE);
output = new BufferedOutputStream(sock.getOutputStream(),BUFFSIZE);
// now wait for test
byte ack[] = new byte[1];
if (VERBOSE) System.out.println("Waiting for test A..." );
input.read(ack);
if (VERBOSE) System.out.println("Test A recieved: "+ack[0]);
}
// recieve a test from the client
public void recv_testB() throws IOException
{
byte ack[] = new byte[1];
if (VERBOSE) System.out.println("Waiting for test B..." );
input.read(ack);
if (VERBOSE) System.out.println("Test B recieved: "+ack[0]);
}
// send a test to the client
public void send_testC() throws IOException
{
int ack = 2;
if (VERBOSE) System.out.println("Sending test C..." );
output.write(ack);
output.flush();
}
// shutdown the socket
public void closesocket() throws IOException
{
output.close();
input.close();
sock.close();
if (VERBOSE) System.out.println("Server: closing socket" );
}
public void send_doubles(double vals[], int len) throws IOException
{
if (VERBOSE) System.out.print("Server: sending " + len +" doubles: " );
/* convert our array of floats into an array of bytes */
ByteArrayOutputStream bytestream;
bytestream = new ByteArrayOutputStream(len*DOUBLE_SIZE);
DataOutputStream out;
out = new DataOutputStream(bytestream);
for (int i=0; i<len; i++) {
out.writeDouble(vals[i]);
if (VERBOSE) System.out.print(vals[i]+" " );
}
byte byte_array[] = bytestream.toByteArray();
byte flip_array[] = new byte[bytestream.size()];
int i, j;
for (i=0; i<len; i++) {
for (j=0; j<DOUBLE_SIZE; j++) {
flip_array[(i+1) * DOUBLE_SIZE - j - 1] = byte_array[i * DOUBLE_SIZE + j];
}
}
output.write(flip_array, 0, bytestream.size());
output.flush();
if (VERBOSE) System.out.println("" );
}
public int recv_doubles(double val[], int maxlen) throws IOException
{
int i;
int numbytes;
int totalbytes = 0;
byte data[] = new byte[maxlen*DOUBLE_SIZE];
/* for performance, we need to receive data as an array of bytes and then convert to an array of doubles */
if (maxlen*8>BUFFSIZE) System.out.println("Sending more doubles then will fit in buffer!" );
while (totalbytes < maxlen*DOUBLE_SIZE) {
numbytes = input.read(data);
// copy the bytes into the result buffer
for (i=totalbytes; i<totalbytes+numbytes; i++)
buff[i] = data[i-totalbytes];
totalbytes += numbytes;
}
// now we must convert the array of bytes to an array of doubles
ByteArrayInputStream bytestream_rev;
DataInputStream instream_rev;
byte flip_array[] = new byte[totalbytes];
int j;
for (i = 0; i < totalbytes / DOUBLE_SIZE; i++) {
for (j = 0; j < DOUBLE_SIZE; j++) {
flip_array[(i+1)*DOUBLE_SIZE - j - 1] = buff[i * DOUBLE_SIZE + j];
}
}
bytestream_rev = new ByteArrayInputStream(flip_array);
instream_rev = new DataInputStream(bytestream_rev);
for (i=0; i<maxlen; i++)
val[i] = instream_rev.readDouble();
if (VERBOSE) {
System.out.print("Server: received " + maxlen + " doubles: " );
for (i=0; i<maxlen; i++)
System.out.print(val[i]+" " );
System.out.println("" );
}
return maxlen;
}
public static void main(String args[]) throws IOException, InterruptedException {
Server s = new Server(5010);
s.connect();
s.recv_testB();
s.send_testC();
double val[] = new double[3];
s.recv_doubles(val, 3);
s.send_doubles(val, 3);
s.closesocket();
}
}
*********************
***** client c ********
*********************
#define BUFFSIZE 64000
#define VERBOSE 1 // turn on or off debugging output
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/wait.h>
typedef struct{
int port; // the port I'm listening on
int new_fd; // new connection on new_fd
struct sockaddr_in serv_addr; // connector's address information
double buffer[BUFFSIZE]; // memory for buffer
double buffer2[BUFFSIZE]; // memory for temporary buffer
} Client;
// create a new client
Client newClient (p, test)
int p, test;
{
Client c;
c.port = p;
if (VERBOSE) printf("Client: opening socket on port = %d \n", c.port);
if ((c.new_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) perror("socket" );
c.serv_addr.sin_family = AF_INET;
c.serv_addr.sin_port = htons(c.port);
c.serv_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(c.serv_addr.sin_zero), 8);
if (connect(c.new_fd, (struct sockaddr *)&c.serv_addr, sizeof(struct sockaddr)) == -1) {
perror("connect new_fd" );
exit(1);
}
// send out test byte
char temp[1];
temp[0] = '.';
if (VERBOSE) printf("Sending test A: %d\n", temp[0]);
send(c.new_fd, temp, 1, 0);
return c;
};
// send a test to the server
void send_testB(c)
Client c;
{
char temp[1];
temp[0] = 1;
if (VERBOSE) printf("Sending test B: %d\n", temp[0]);
send(c.new_fd, temp, 1, 0);
};
// recieve a test from the server
void recv_testC(c)
Client c;
{
char temp[1];
int total = 0;
if (VERBOSE) printf("Waiting for test C...\n" );
while (total < 1){
total += recv(c.new_fd, temp, 1, 0);
}
if (VERBOSE) printf("Test C recieved: %d\n",temp[0]);
};
// shut down the socket
void closesocket(c)
Client c;
{
if (VERBOSE) printf("Client: closing socket\n" );
close(c.new_fd);
};
// send some doubles over the wire
void send_doubles(c, val, len)
Client c; double *val; int len;
{
double *buff;
char *ptr, *valptr;
int i,j;
if (send(c.new_fd, (char *) val, len, 0) == -1)
perror("send doubles" );
len = len/sizeof(double);
if (VERBOSE) {
printf("Client: sending %d doubles: ", len);
for (i=0; i<len; i++)
printf("%0.3f ", val[i]);
printf("\n" );
}
};
// receive some doubles, returns num of doubles received
int recv_doubles(c, val, maxlen)
Client c; double *val; int maxlen;
{
int numbytes = 0;
int i, j;
char *temp;
char *result;
int end = 0;
int total_bytes = 0;
temp = (char *) c.buffer;
result = (char *) c.buffer2;
j = 0;
// we receiving the incoming doubles one byte at a time
// oh cross language sockets are so much fun...
while (!end) {
if ((numbytes=recv(c.new_fd, temp, BUFFSIZE, 0))==-1)
perror("recv" );
for (i=0; i<numbytes; i++) {
result[j] = temp[i];
j++;
}
total_bytes = total_bytes + numbytes;
if (total_bytes==maxlen)
end = 1;
}
// now we need to put the array of bytes into the array of floats
char *ptr;
int num = j/sizeof(double);
ptr = (char *) val;
for (i = 0; i < j; i++)
ptr[i] = result[i];
if (VERBOSE) {
printf("Client: received %d doubles: ", num);
for (i=0; i<num; i++)
printf("%e ", val[i]);
printf("\n" );
}
return num;
};
void main()
{
Client c = newClient(5010,2);
send_testB(c);
recv_testC(c);
double val[3]; val[0]=1.2; val[1]=2.3; val[2]=3.4;
printf("sizeof(val) = %d\n", sizeof(val));
printf("sizeof(double) = %d\n", sizeof(double));
send_doubles(c, val, sizeof(val));
recv_doubles(c, val, sizeof(val));
closesocket(c);
}
Message édité par superpioupiou le 12-06-2008 à 02:50:16