GreetingClient kodunu yazdırmak için java soket istemci/sunucu kodunu değiştirme:kullanıcının girişini ve tersi
import java.net.*;
import java.io.*;
public class GreetingClient
{
public static void main(String [] args)
{
String serverName = args[0];
int port = Integer.parseInt(args[1]);
try{
System.out.println("Connecting to " + serverName + " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to " + client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Hello from "+ client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
client.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
GreetingServer kodu
import java.net.*;
import java.io.*;
public class GreetingServer extends Thread{
private ServerSocket serverSocket;
public GreetingServer(int port) throws IOException{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(6000000);
}
public void run(){
while(true){
try{
System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to "+ server.getRemoteSocketAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress() + "\nGoodbye!");
server.close();
}
catch(SocketTimeoutException s){
System.out.println("Socket timed out!");
break;
}
catch(IOException e){
e.printStackTrace();
break;
}
}
}
public static void main(String [] args){
int port = Integer.parseInt(args[0]);
try{
Thread t = new GreetingServer(port);
t.start();
}
catch(IOException e){
e.printStackTrace();
}
}
}
//here is the code i want to insert in the client part. this code lets the user type a string (that i want to let it appear on the server side) and asks the user if he want to communicate with the server again.
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class com {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = null;
Scanner sc =new Scanner(System.in);
boolean yes = true;
do{
System.out.println("What do you want to say to the server?");
String toServer = sc.nextLine();
System.out.println ("The client said: " + toServer);
System.out.println("Do you wish to continue(y/n)?");
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if(s.equals("n")){
yes=false;
}
}
while (yes== true);
}
}
kod mükemmel ama ben want.first olarak çalışıyor Yukarıdaki kodu (son yorumlardan önce) karşılama kullanıcılarına dahil etmek için eklemek istiyorum. İstemci sunucusu iletişimi kurmak istiyorum. GreetingServer ve GreetingClient bölümünü kopyalayıp yapıştırdım. ancak istemci ve sunucu arasında bağlantı kurulduğunda, GreetingServer ve GreetingClient kodlarından gelen Dizeler görünür. istediğim bu değil. Kullanıcının girdisini GreetingClient'ten GreetingServer'a çıkarmak istiyorum. kullanici girdisi: hi server Sunucuda açilmalidir: hi server
Sorunuz nedir? Ne sorduğun belli değil. Kod çalışmıyor mu? Bir istisna veya hata mesajı var mı? Gönderinizi düzenleyebilir ve yığın takibini ekleyebilir misiniz? –
Bu kod, doğrudan [TutorialsPoint] adresinden (http://www.tutorialspoint.com/java/java_networking.htm) atıfta bulunulmadan alınır. Bu intihara karşı StackOverflow politikasının bir ihlalidir. Lütfen [Yığın Değişim SSS] 'ni okuyun (http://meta.stackexchange.com/questions/160077/users-are-calling-me-a-plagiarist-what-do-i-do) ve takip edin. Ve lütfen * kendi kodunuz * ile ilgili StackOverflow ile ilgili sorular sorun. – RealSkeptic