[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Socket



I dont see anything wrong with your code :)
I recomend the Java Tutorial (Which I guess you're already using)
http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html

The next post says you solved the problem, Mind sharing with us ?


ak.

Thank you Ahmad for your help.
I tryed with an other example and I changed the port (with the port 8888, I obtained an error messages, I don't know why). It's the following example :
 
// client

import java.net.*;

import java.io.*;

public class ClientSock {

public static void main(String[] args) {

String host = "localhost";

int port = 80;  // ne marche pas avec le port 8888

StringBuffer instr = new StringBuffer();

String TimeStamp;

System.out.println("SocketClient initialized");

try {

/** Obtain an address object of the server */

InetAddress address = InetAddress.getByName(host);

/** Establish a socket connetion */

Socket connection = new Socket(address, port);

/** Instantiate a BufferedOutputStream object */

BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());

/** Instantiate an OutputStreamWriter object with the optional character  encoding.*/

OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");

TimeStamp = new java.util.Date().toString();

String process = "Calling the Socket Server on "+host+" port "+port+" at "+ TimeStamp +(char)13;

/** Write across the socket connection and flush the buffer */

osw.write(process);

osw.flush();

/** Instantiate a BufferedInputStream object for reading incoming socket streams.*/

BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());

/**Instantiate an InputStreamReader with the optional character encoding.*/

InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");

/**Read the socket's InputStream and append to a StringBuffer */

int c;

while ( (c = isr.read()) != 13)

instr.append( (char) c);

/** Close the socket connection. */

connection.close();

System.out.println(instr);

}

catch (IOException f) {

System.out.println("IOException: " + f);

}

catch (Exception g) {

System.out.println("Exception: " + g);

}

}

}

// Server

import java.net.*;

import java.io.*;

import java.util.*;

public class ServeurSock {

static ServerSocket socket1;

protected final static int port = 80;

static Socket connection;

static boolean first;

static StringBuffer process;

static String TimeStamp;

public static void main(String[] args) {

try{

socket1 = new ServerSocket(port);

System.out.println("SingleSocketServer Initialized");

int character;

while (true) {

connection = socket1.accept();

BufferedInputStream is = new BufferedInputStream(connection.getInputStream());

InputStreamReader isr = new InputStreamReader(is);

process = new StringBuffer();

while((character = isr.read()) != 13) {

process.append((char)character);

}

System.out.println(process);

//need to wait 10 seconds for the app to update database

try {

Thread.sleep(10000);

}

catch (Exception e){}

TimeStamp = new java.util.Date().toString();

String returnCode = "SingleSocketServer repsonded at "+ TimeStamp + (char) 13;

BufferedOutputStream os = new BufferedOutputStream(connection.getOutputStream());

OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");

osw.write(returnCode); 

osw.flush();

}

}

catch (IOException e) {}

try {

connection.close();

}

catch (IOException e) {}

}

}

I have an other problem with an SQL request, when I run the following request, no change happen in the database:
 
 RS = S.executeQuery("DELETE FROM Client where Name NOT IN (select ClientName from Product)");
The other queries run successfully.
 


 


Appel audio GRATUIT partout dans le monde avec le nouveau Yahoo! Messenger
Téléchargez le ici !