Witam, mam problem z programem(programami) które muszę napisać na ćwiczenia, a mianowicie Client - Server, zadanie jest proste client ma wysłać dwie liczby, server je odebrać pomnożyć i odesłać z powrotem, zadanie proste jak się wydaje ale nie dla kogoś kto nigdy nie programował w javie.
Doszedłem do momentu gdzie Client-Server nawiązują połączenie i jest wszystko ok bo server odpowiada na zapytanie clienta i odsyła mu wiadomość o tym, problem powstaje w momencie tak mi się wydaje wysyłania pobranych od użytkownika liczb na server, ponieważ server już nie odpowiada na to tylko stoi w miejscu i nic nie robi. Nie znam się na javie i nie mogę dojść do tego czemu, robię podobnie jak wysyłaniu wcześniejszej wiadomości która normalnie jest dostarczana ale już przy tych liczbach mam problem i tutaj proszę o waszą pomoc lub wskazówkę bo szybciej mi pokażecie gdzie problem się znajduje dzięki :)
Tutaj dwa programy, troszkę długie ale co zrobić.
Server:
//package bdn ;
import java.net.* ;
import java.io.* ;
public class Server {
static ServerSocket socket1 ;
protected final static int port = 19999 ;
static Socket connection ;
static boolean first ;
static StringBuffer process ;
static StringBuffer msg = new StringBuffer();
static String TimeStamp ;
static int first_var, second_var, result ;
public static void main(String args[]) {
try {
socket1 = new ServerSocket(port) ;
System.out.println("Server 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) ;
TimeStamp = new java.util.Date().toString() ;
String returnCode = "Server responded at " + TimeStamp + (char)13 ;
BufferedOutputStream os = new BufferedOutputStream(connection.getOutputStream()) ;
OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII") ;
osw.write(returnCode) ;
osw.flush() ;
BufferedInputStream is_2 = new BufferedInputStream(connection.getInputStream()) ;
InputStreamReader isr_2 = new InputStreamReader(is_2) ;
while((character = isr_2.read()) != 13) {
msg.append((char)character) ;
}
first_var = Integer.parseInt(msg.toString()) ;
System.out.println("Otrzymano od clienta: " + first_var) ;
BufferedInputStream is_3 = new BufferedInputStream(connection.getInputStream()) ;
InputStreamReader isr_3 = new InputStreamReader(is_3) ;
while((character = isr_3.read()) != 13) {
msg.append((char)character) ;
}
second_var = Integer.parseInt(msg.toString()) ;
System.out.println("Otrzymano od clienta: " + second_var) ;
result = first_var * second_var ;
String msg2 = "Odpowiedz serwera | " + first_var + " * " + second_var + " = " + Integer.toString(result) ;
osw.write(msg2) ;
osw.flush();
}
}
catch(IOException e) { }
try{
connection.close();
}
catch(IOException e) { }
}
}
Client:
//package bdn;
import java.lang.String.*;
import java.net.*;
import java.io.*;
public class Client {
public static void main(String args[]) {
// Host server
String host = "localhost" ;
// Port
int port = 19999 ;
StringBuffer instr = new StringBuffer() ;
String TimeStamp ;
System.out.println("Socket Client initialized") ;
// REQUESTING A SOCKET AND ESTABILISHING A CONNECTION
try {
// Obtain an address object of the server
InetAddress address = InetAddress.getByName(host) ;
// Estabilish a socket connection
Socket connection = new Socket(address, port) ;
// WRITING TO THE SOCKET
// Instantiate a BuffereOutputStream 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() ;
// READING FROM THE SOCKET
// 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 InputStream and append to a StringBuffer
int c ;
while( (c = isr.read()) != 13) {
instr.append( (char)c ) ;
}
System.out.println(instr) ;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)) ;
System.out.print("Podaj pierwsza liczbe: ") ;
String s = stdin.readLine() ;
osw.write(s) ; // send first variable
osw.flush();
try {
Thread.sleep(1000) ;
} catch(Exception e) {}
System.out.print("Podaj druga liczbe: ") ;
String s2 = stdin.readLine() ;
osw.write(s2) ; // send second variable
osw.flush();
BufferedInputStream bis2 = new BufferedInputStream(connection.getInputStream()) ;
// Instantiate an InputStreamReader with the optional character encoding
InputStreamReader isr2 = new InputStreamReader(bis2, "US-ASCII") ;
while( (c = isr2.read()) != 13) {
instr.append( (char)c ) ;
}
System.out.println(instr) ;
// Close the Socket connection
connection.close() ;
}
catch(IOException f) {
System.out.println("IOException: " + f) ;
}
catch(Exception g) {
System.out.println("Exception: " + g) ;
}
}
}