서버를 실행할 때 클라이언트 서버 응용 프로그램이 있습니다. 서버 클래스에서 인쇄 할 때 얻을 수 있지만 Client 클래스를 실행하면 연결이 설정된 경우에도 아무 것도 발생하지 않습니다. 클라이언트 클래스처럼 보입니다. 여기 클라이언트 서버가 통신하지 않습니다
서버
에서 모든 메시지를 수신하지 않는 것은 내 서버 코드import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
ServerSocket provider;
Socket clientsocket;
OutputStream out;
InputStream in;
InetAddress clientAddress;
StringBuilder sb;
BufferedReader br;
String msg="";
Server(){
}
public void Communicate()
{
try {
provider=new ServerSocket(2013);
System.out.println("Server Waiting for connections");
clientsocket=provider.accept();
System.out.println("incoming connection from ");
clientAddress=clientsocket.getInetAddress();
//System.out.println("Client Name"+clientAddress.getHostName());
System.out.println("Client Address "+clientAddress.getHostAddress());
out=clientsocket.getOutputStream();
in=clientsocket.getInputStream();
sendmsg("connection success");
do
{
msg=read_instream(in);
System.out.println("client say"+msg);
if(msg.equals("bye"))
{
sendmsg(msg);
clientsocket.close();
in.close();
out.close();
}
}
while(!msg.equals("bye"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sendmsg(String msg) throws IOException
{
out.write(msg.getBytes());
out.flush();
}
private String read_instream(InputStream in) throws IOException
{
br=new BufferedReader(new InputStreamReader(in));
sb=new StringBuilder();
while (br.readLine()!=null)
sb.append(br.readLine());
return sb.toString();
}
public static void main(String[] args){
Server provider=new Server();
while(true)
provider.Communicate();
}
}
와 클라이언트 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
Socket client;
String msg="give me the last degree";
OutputStream out;
InputStream in;
StringBuilder sb;
BufferedReader br;
public void Communicate()
{
try {
client=new Socket("localhost",2013);
out=client.getOutputStream();
in=client.getInputStream();
System.out.println("Messege from server:"+read_instream(in));
while (!msg.equals("bye"))
{
msg=read_instream(in);
System.out.println("server say"+msg);
sendmsg("hey from client");
msg="bye";
sendmsg(msg);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String read_instream(InputStream in) throws IOException
{
br=new BufferedReader(new InputStreamReader(in));
sb=new StringBuilder();
while (br.readLine()!=null)
sb.append(br.readLine());
return sb.toString();
}
private void sendmsg(String msg) throws IOException
{
out.write(msg.getBytes());
out.flush();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Client requester=new Client();
requester.Communicate();
}
}
sry 나는 코드에서 무엇을해야하는지 알 수있는 답변을 얻지 못했습니다? – Tony
나는 그것을 사용할 때마다 닫았지만 아무 것도 바뀌지 않았다. – Tony