Ceriwis  

Go Back   Ceriwis > HOBI > Komputer & Teknologi > Programming

Programming Share, tanya jawab, saling bantu antar programmer dengan berbagai macam bahasa pemrograman.

Reply
 
Thread Tools
  #1  
Old 11th November 2011
Braincode's Avatar
Braincode Braincode is offline
Ceriwis Addicted
 
Join Date: Nov 2011
Posts: 4,638
Rep Power: 20
Braincode mempunyai hidup yang Normal
Default [NEED HELP] java: kenapa ngak mo nge-loop yah program nya?

mo minta tolong neeh...

gw lagi bikin program client-server sederhana pake socket, di java.



user input kata kunci tertentu lalu server membalas dengan jawaban yang sudah tersedia, sampai QUIT dan program berhenti.

kata kunci:

MY NAME IS ____

TIME

JOKE TIME

QUIT

selain itu server akan balas I don't understand...



program terdiri dari 3 class (harus demikian): Server, Client, MyConnection (berisi perintah untuk Send dan Receive message)



masalahnya: seharusnya program terus berjalan sampai user memasukkan QUIT dan server balas Goodbye. tapi programnya mandek setelah satu input. ngak tau apa yang salah ??? (agak newbie juga di java)



berikut code nya...




Code:

import java.io.*;
import java.net.*;

public class Client {
public static void main(String[] args) throws IOException {
Socket hiSocket = null;

System.out.println("Client tries to connect to server...");

try {
hiSocket = new Socket("127.0.0.1", 4444);
} catch (UnknownHostException e) {
System.err.println("Don't know about host: laboratory.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: laboratory.");
System.exit(1);
}

System.out.println("Client connected!");

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

String fromUser;
String fromServer;

try {
MyConnection myco = new MyConnection(hiSocket);

do {
System.out.print("Enter Message: ");
fromUser = stdIn.readLine();
myco.Send(fromUser);

while((fromServer = myco.Receive()) != null)
System.out.println("Server: " + fromServer);
} while (!(fromServer.equals("Goodbye")));
} catch (Exception e) {
System.err.println("Connection to server error.");
System.exit(1);
}

stdIn.close();
hiSocket.close();
}
}


Code:

import java.net.*;
import java.io.*;

public class MyConnection {
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;

public MyConnection(Socket socket) throws Exception {
out = new PrintWriter(socket.getOutputStream(), true);

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}

public void Send(String msg) throws Exception {
out.println(msg);
out.flush();
}

public String Receive() throws Exception {
return in.readLine();
}
}


Code:

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

public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}

Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}

Date now = new Date();
String inputLine;

try {
MyConnection myco = new MyConnection(clientSocket);

while ((inputLine = myco.Receive()) != null) {
if (inputLine.startsWith("MY NAME IS")) {
String name;

name = inputLine.substring(11);
myco.Send("Hello, " + name);
} else if (inputLine.equals("TIME"))
myco.Send(now.toString());
else if (inputLine.equals("JOKE TIME")) {
myco.Send("Jamaican + Italian = Lil' Pastafarians");
myco.Send("Filipino + Hollander = Lil' Jalapenos");
myco.Send("French + Greek = Lil' Freeks");
myco.Send("Icelandian + Cuban = Lil' Ice cubes");
} else if (inputLine.equals("QUIT")) {
myco.Send("Goodbye");
} else
myco.Send("I can't understand what you're saying");
inputLine = "";
}
} catch (Exception e) {
System.err.println("Connection to client error.");
System.exit(1);
}

clientSocket.close();
serverSocket.close();
}
}

makasih sebelumnya buat yang bisa tolong...



Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


 


All times are GMT +7. The time now is 03:22 AM.


no new posts