πρόβλημα με Socket

Συζητήσεις για την Java

Συντονιστές: WebDev Moderators, Super-Moderators

Απάντηση
VagosM
Δημοσιεύσεις: 46
Εγγραφή: 03 Νοέμ 2010 20:19

πρόβλημα με Socket

Δημοσίευση από VagosM » 15 Μαρ 2013 00:44

Για σας έχω να κάνω έναν τηλεφωνικό κατάλογο με socket με της παρακάτω εντολές

Εικόνα

Ο κλιεντ

Κώδικας: Επιλογή όλων

import java.net.*;
import java.io.*;
import java.util.Scanner;

public class Client {
	 
public static void main(String[] args) {
	
	ObjectOutputStream outstream=null;
	ObjectInputStream instream=null;
	
try{
Socket socket = new Socket("localhost", 5555);

//ObjectOutputStream outstream =new ObjectOutputStream(socket.getOutputStream());
//ObjectInputStream instream =new ObjectInputStream(socket.getInputStream());
BufferedReader in = new BufferedReader (new InputStreamReader(socket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

System.out.println("Sending contacts to the Server...");
System.out.println("Connecting to "+ socket.getInetAddress()+ " and port "+socket.getPort());
System.out.println("Local Address :"+socket.getLocalAddress()+" Port:"+socket.getLocalPort());
String name,surName,job,street;
int number;
String strin=null;
Scanner input = new Scanner(System.in);

out.write("START"+"\n");
out.flush();

strin = in.readLine();
//System.out.println("The server says: "+strin);
if (strin.equals("WAITING")){
	outstream =new ObjectOutputStream(socket.getOutputStream());
	instream =new ObjectInputStream(socket.getInputStream());
	System.out.println("The server says: "+strin);
	System.out.print("Write the contact elements ");
	System.out.print("Write the name: ");
	name = input.nextLine();
	System.out.print("Write the surname: ");
	surName = input.nextLine();
	System.out.print("Write the job: ");
	job = input.nextLine();
	System.out.print("Write the street: ");
	street = input.nextLine();
	System.out.print("Write the phone number: ");
	number = input.nextInt();

	PhoneBook phone=new PhoneBook(name,surName,job,street,number);
	outstream.writeObject(phone+"\n");
	outstream.flush();
	out.write("REQUEST_SEARCH"+"\n");
    out.flush();
}
//strin= in.readLine();
//if(strin.equals("RECORDS")){
	
//}
strin= in.readLine();
if (strin.equals("ΝΧRECORD")){
	System.out.println("The server says: "+strin);
	out.write("REQUEST_INSERT"+"\n");
}
strin= in.readLine();
if (strin.equals("OK")){
	System.out.println("The server says: "+strin);
	out.write("END"+"\n");
	in.close();
	out.close();
	out.flush();
	socket.close();
	System.out.println("Connection Closing...");
	}

}
catch (Exception ex){
System.out.println("Connection Refused!!!");
}
}
}
ο σεβερ

Κώδικας: Επιλογή όλων

import java.net.*;
import java.util.ArrayList;
import java.io.*;

public class ServerClient  {
	 
public static void main(String[] args) {
	ArrayList <PhoneBook> listPhone=new ArrayList<PhoneBook>&#40;&#41;;	
	PhoneBook pB = new PhoneBook&#40;&#41;;
	ObjectInputStream instream;
	ObjectOutputStream outstream ;
try&#123;
	
ServerSocket server = new ServerSocket&#40;5555,50&#41;;

System.out.println&#40;"Waiting Incoming Connection..."&#41;;
System.out.println&#40;"Local Address &#58;"+server.getInetAddress&#40;&#41;+" Port&#58;"+server.getLocalPort&#40;&#41;&#41;;

Socket socket = server.accept&#40;&#41;;

//ObjectOutputStream outstream =new ObjectOutputStream&#40;socket.getOutputStream&#40;&#41;&#41;;
//ObjectInputStream instream =new ObjectInputStream&#40;socket.getInputStream&#40;&#41;&#41;;
BufferedReader in = new BufferedReader &#40;new InputStreamReader&#40;socket.getInputStream&#40;&#41;&#41;&#41;;
BufferedWriter out = new BufferedWriter&#40;new OutputStreamWriter&#40;socket.getOutputStream&#40;&#41;&#41;&#41;;
String strin=null;

//pB = &#40;PhoneBook&#41; instream.readObject&#40;&#41;;
//String sName = pB.getsurName&#40;&#41;; 


strin= in.readLine&#40;&#41;;
//System.out.println&#40;"The client says&#58; "+strin&#41;;	
if&#40;strin.equals&#40;"START"&#41;&#41;&#123;
System.out.println&#40;"The client says&#58; "+strin&#41;;	
out.write&#40;"WAITING"+"\n"&#41;;
out.flush&#40;&#41;;
&#125;
strin= in.readLine&#40;&#41;;
if&#40;strin.equals&#40;"REQUEST_SEARCH"&#41;&#41;&#123;
	System.out.println&#40;"The client says&#58; "+strin&#41;;	
	outstream =new ObjectOutputStream&#40;socket.getOutputStream&#40;&#41;&#41;;
	instream =new ObjectInputStream&#40;socket.getInputStream&#40;&#41;&#41;;
	pB = &#40;PhoneBook&#41; instream.readObject&#40;&#41;;
	String sName = pB.getsurName&#40;&#41;; 
	for&#40;int i=0;i<listPhone.size&#40;&#41;;i++&#41;
      if&#40;listPhone.get&#40;i&#41;.getsurName&#40;&#41;.equals&#40;sName&#41;&#41;
	  outstream.writeObject&#40;pB&#41;;
	  outstream.flush&#40;&#41;;
	  instream.close&#40;&#41;;
	  outstream.close&#40;&#41;;
	  out.write&#40;"RECORSDS"+"\n"&#41;;
	  out.flush&#40;&#41;;
&#125;else&#123;
out.write&#40;"ΝΧRECORD"+"\n"&#41;;
out.flush&#40;&#41;;
&#125;
strin= in.readLine&#40;&#41;;
if&#40;strin.equals&#40;"REQUEST_INSERT"&#41;&#41;&#123;
	listPhone.add&#40;pB&#41;;
	out.write&#40;"OK"+"\n"&#41;;
	out.flush&#40;&#41;;
&#125;
strin= in.readLine&#40;&#41;;
if&#40;strin.equals&#40;"END"&#41;&#41;&#123;
System.out.println&#40;"The client says&#58; "+strin&#41;;	
in.close&#40;&#41;;
out.close&#40;&#41;;
socket.close&#40;&#41;;

System.out.println&#40;"Connection Closing..."&#41;;&#125;
&#125;

catch &#40;Exception ex&#41;&#123;
System.out.println&#40;"Error during I/O"&#41;;
ex.getMessage&#40;&#41;;
ex.printStackTrace&#40;&#41;;
&#125;&#125;&#125;
Δεν μπορώ να βρω το λάθος που κάνω λάθος και κολάει και δεν μπορώ να περάσω έγραφες και να κάνω αναζήτηση.
ευχαριστώ εκ τον προτέρων

Απάντηση

Επιστροφή στο “Java”

Μέλη σε σύνδεση

Μέλη σε αυτήν τη Δ. Συζήτηση: Δεν υπάρχουν εγγεγραμμένα μέλη και 2 επισκέπτες