서버가 클라이언트를 청취 한 다음 들어오는 데이터의 대문자를 제공하는 TCP/IP를 기반으로 기본 프로젝트를 만듭니다.간단한 클라이언트 서버 프로그램
Server.java :
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(7948);
Socket s= ss.accept();
System.out.print("Server connected\n");
BufferedInputStream bis = new BufferedInputStream (s.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream (s.getOutputStream());
while(true)
{
int a = bis.available();
byte b[] = new byte[a];
bis.read(b);
String str = new String(b);
str = str.toUpperCase();
b = str.getBytes();
bos.write(b,0,b.length);
bos.flush();
if(str.equals("BYE"))
break;
else
continue;
}
System.out.print("\nServer Disconnecting");
String str = "Adios Amigo";
bos.write(str.getBytes());
bos.flush();
bis.close();
bos.close();
ss.close();
s.close();
}
}
Client.java :이 프로그램은 한 가지 문제를 제외하고 제대로 작동
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String[] args) throws Exception
{
BufferedReader clientStream = new BufferedReader(new InputStreamReader(System.in));
String str;
int a;
byte[] b;
Socket s = new Socket(InetAddress.getLocalHost(), 7948);
BufferedOutputStream bos = new BufferedOutputStream (s.getOutputStream());
BufferedInputStream bis = new BufferedInputStream (s.getInputStream());
one:while(true)
{
str = clientStream.readLine();
b =str.getBytes();
bos.write(b);
bos.flush();
a=bis.available();
b = new byte[a];
bis.read(b);
str = new String (b);
str.trim();
System.out.print("The server says: "+str);
if (str.equals("BYE"))
{
bis.read(b);
str = new String (b);
System.out.print("The server says: "+str);
break one;
}
}
s.close();
clientStream.close();
bos.close();
bis.close();
}
}
, 클라이언트 측에서 출력이 두 개의 입력 후 제공됩니다. 즉, 첫 번째 출력을 얻으려면 클라이언트 쪽에서 두 개의 입력을 제공해야한다는 것을 의미합니다. 버그를 추적 할 수 없습니다. 아무도 도와 줄 수 있습니까?
미래에 변수 이름을 지정하면서 알렉스 내가 마음에 당신의 제안을하겠습니다 감사합니다 :) – Rajat