Client and server data transfer

server side code

import java.io.*;

import java.net.*;

import java.sql.*;

publicclass Server{

publicstaticvoid main (String args[]){

Input in;

Output out;

Socket socket;

ServerSocket serverSocket;

Statement stmt =null;

Connection conn =null;

try{

// setup server socket to listen on port 10000

serverSocket =new ServerSocket (10000);

socket = serverSocket.accept ();

in =new Input (socket.getInputStream());

out =new Output (socket.getOutputStream());

Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");

conn = DriverManager.getConnection ("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:/java/db.mdb");

stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

in.start ();

out.start ();

}

catch (Exception ex){

System.err.println (ex.getMessage());

}

}

}

class Inputextends Thread{

String tmpData;

BufferedReader br;

Input (InputStream in){

br =new BufferedReader (new InputStreamReader (in));

tmpData ="";

}

publicvoid run (){

try{

while ((tmpData = br.readLine ()) !=null){

System.out.println ("[REMOTE] " + tmpData);

}

}

catch (Exception ex){

System.err.println ("ERROR: " + ex.getMessage ());

}

}

publicvoid close (){

try{

br.close ();

}

catch (Exception ex){

System.err.println ("ERROR: " + ex.getMessage ());

}

}

}

class Outputextends Thread{

String tmpData;

BufferedReader br;

PrintStream ps;

Output (OutputStream out){

ps =new PrintStream (out);

br =new BufferedReader (new InputStreamReader (System.in));

}

publicvoid run (){

try{

while ((tmpData = br.readLine ()) !=null){

ps.println (tmpData);

ps.flush ();

if (tmpData.equalsIgnoreCase ("exit")){

System.exit (0);

}

}

}

catch (Exception ex){

System.err.println ("ERROR: " + ex.getMessage());

}

}

publicvoid close (){

try{

ps.close ();

br.close ();

}

catch (Exception ex){

System.err.println ("ERROR: " + ex.getMessage());

}

}

}

Client Side code

import java.io.*;

import java.net.*;

publicclass Client{

publicstaticvoid main(String args[]){

Input in;

Output out;

Socket socket;

PrintStream ps;

int userChoice = 0;

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

do{

try{

socket =new Socket("localhost", 10000);

out =new Output(socket.getOutputStream());

}catch (UnknownHostException e){

System.out.println("Cannot Find The Server");

System.exit(1);

}catch (IOException e){

System.out.println("No I/O");

System.exit(1);

}

System.out.println("1 - Add Contact");

System.out.println("2 - View Contact");

System.out.println("3 - Exit");

System.out.print("Option: ");

userChoice = Integer.parseInt(br.readLine());

if (userChoice == 1){

String strName, strCourse, tmp;

try{

System.out.print("Enter name: ");

strName = br.readLine();

System.out.print("Enter course: ");

strCourse = br.readLine();

tmp = ("INSERT INTO [Students]([Name],[Course]) VALUES ('"+strName+"', '"+strCourse+"')");

ps =new PrintStream(out);

ps.println(tmp);

ps.flush();

ps.close();

}

catch (Exception ex){

System.err.printf("ERROR: " + ex.getMessage());

}

}elseif (userChoice == 2){

}elseif (userChoice != 3){

System.out.println("ERROR: Invalid Choice.");

}

System.out.println();

}while (userChoice != 3);

}//main close

}//client close

[8778 byte] By [Mr_Chena] at [2007-10-3 4:37:31]
# 1
my problem is i cannot sent the tmp to my server side.
Mr_Chena at 2007-7-14 22:41:15 > top of Java-index,Core,Core APIs...
# 2

Well you haven't got any code that will do it. You're creating an Output at the client but you're never starting it, so it does nothing, and you have a loop in the client's main() procedure that reads and sends one line from System.in then closes the output socket, so you will never be able to send > 1 line from that.

So I agree, you can't.

ejpa at 2007-7-14 22:41:15 > top of Java-index,Core,Core APIs...
# 3

i change it like this liao

but still cannot

import java.io.*;

import java.net.*;

public class Client {

public static void main (String args[]) throws IOException {

Input in;

Output out;

Socket socket;

try{

socket = new Socket("localhost", 10000);

out = new Output (socket.getOutputStream());

out.start ();

} catch (UnknownHostException e) {

System.out.println("Cannot Find The Server");

System.exit(1);

} catch (IOException e) {

System.out.println("No I/O");

System.exit(1);

}

int userChoice = 0;

BufferedReader br = new BufferedReader (new InputStreamReader (System.in));

//in.start ();

do {

System.out.println ("1 - Add Contact");

System.out.println ("2 - View Contact");

System.out.println ("3 - Exit");

System.out.print ("Option: ");

userChoice = Integer.parseInt (br.readLine());

//socket = new Socket("localhost", 10000);

//out2 = new Output(socket.getOutputStream());

if (userChoice == 1) {

String strName, strCourse, tmp;

try {

System.out.print ("Enter name: ");

strName = br.readLine();

System.out.print ("Enter course: ");

strCourse = br.readLine ();

tmp = ("INSERT INTO [Students]([Name],[Course]) VALUES ('"+strName+"', '"+strCourse+"')");

//PrintStream ps = new PrintStream(tmp);

//ps.flush();

}

catch (Exception ex) {

System.err.printf ("ERROR: " + ex.getMessage());

}

}

else if (userChoice == 2) {

}

else if (userChoice != 3) {

System.out.println ("ERROR: Invalid Choice.");

}

System.out.println ();

} while (userChoice != 3);

}//main close

}//client close

class Output extends Thread {

String tmpData, tmp;

BufferedReader br;

PrintStream ps;

Output (OutputStream out) {

ps = new PrintStream (out);

br = new BufferedReader (new InputStreamReader (System.in));

}

public void run () {

try {

while ((tmpData = tmp) != null) {

ps.println (tmpData);

ps.flush ();

if (tmpData.equalsIgnoreCase ("exit")) {

System.exit (0);

}

}

}

catch (Exception ex) {

System.err.println ("ERROR: " + ex.getMessage());

}

}

public void close () {

try {

ps.close ();

br.close ();

}

catch (Exception ex) {

System.err.println ("ERROR: " + ex.getMessage());

}

}

}

Mr_Chena at 2007-7-14 22:41:15 > top of Java-index,Core,Core APIs...
# 4
Get rid of the Output thread in your client, I don't see the point of that, and just write the data directly to the socket where you have commented it out.
ejpa at 2007-7-14 22:41:15 > top of Java-index,Core,Core APIs...
# 5
Sorry i just learn the Java not very understand what u mean can you show it clearlyThanks u
Mr_Chena at 2007-7-14 22:41:15 > top of Java-index,Core,Core APIs...