[英]Recive socket data java
Why it not recive data from server and from client to server?为什么它不从服务器和从客户端到服务器接收数据? My method "odbierz" is bad?
我的方法“odbierz”不好? I would like sent data from server to client, then client sent data to server on change.
我想将数据从服务器发送到客户端,然后客户端在更改时将数据发送到服务器。
Client class客户类
public class Klient {
private Socket sock;
private PrintWriter out;
private BufferedReader in;
public Klient() throws UnknownHostException, IOException
{
sock=new Socket("localhost",50007);
System.out.println("Nawiazalem polaczenie: "+sock);
in=new BufferedReader(new InputStreamReader(sock.getInputStream()));
out=new PrintWriter(sock.getOutputStream(), true);
}
public void wyslijDane(int numerKart) throws IOException
{
System.out.print("<Wysylamy:> ");
out.print(numerKart);
out.flush();
System.out.println("Wyslano kartę" + numerKart);
}
public void odbierz() throws IOException
{
String str;
if(in.ready())
{
while(true)
{
str=in.readLine();
System.out.println("<Nadeszlo:> " + str);
}
}
else
System.out.println("Zajetre");
}
public static void main(String[] args) throws IOException
{
}
}
public class Server
{
public static final int PORT=50007;
private ServerSocket server;
private Socket sock;
private PrintWriter out;
private BufferedReader in;
public Server() throws IOException
{
server=new ServerSocket(PORT);
System.out.println("Nasluchuje: "+ server);
sock=server.accept();
System.out.println("Jest polaczenie: "+sock);
in=new BufferedReader(new InputStreamReader(sock.getInputStream()));
out=new PrintWriter(sock.getOutputStream(), true);
public void wyslijDane(int numerKarty ) throws IOException
{
System.out.print("<Wysylamy:> ");
out.print(numerKarty);
out.flush();
System.out.println("Wyslano kartę" + numerKarty);
}
}
I don't know how and where you are calling the odbierz()
method, but while(true)
is an infinite loop, that's probably why your program is not working.我不知道您如何以及在何处调用
odbierz()
方法,但是while(true)
是一个无限循环,这可能就是您的程序无法运行的原因。
Try removing the loop:尝试删除循环:
public void odbierz() throws IOException {
String str;
if(in.ready()) {
str=in.readLine();
System.out.println("<Nadeszlo:> " + str);
} else {
System.out.println("Zajetre");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.