从套接字java读取数据 [英] Reading data from a socket java

查看:65
本文介绍了从套接字java读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在尝试从套接字读取数据.我已经建立了一个连接并向另一个正在侦听的套接字发送了一条消息,但它似乎没有收到任何消息.客户端代码:

So, I'm trying to read data from a socket. I have established a connection and sent a message to the other socket which is listening for such, but it doesn't seem to recieve anything. Client code:

import java.net.*;
import java.io.*;
import java.util.*;
public class TCPClient {
 /*Conecting to server.
 */
 public static void Client(String[] args){
  int port = 47361;
  Scanner in = new Scanner(System.in);
  System.err.print("Starting up...");
  Socket CS1 = null; //Declaring CS1 as a socket
  DataOutputStream DOS1 = null; //Declaring an outputput data stream "DOS1"
  DataInputStream DIS1 = null; //Setting the values to null
  System.err.println("All created.");
  System.out.println("---------");
  System.out.println(" OPTIONS");
  System.out.println("---------");
  System.out.println("0. Connect.");
  System.out.println("1. Change port.");
  System.out.println("2. Cancel start-up.");
  System.out.println("");
  System.out.println("Choose an option.");
  try {
   short ans = in.nextShort();
   if (ans == 2){
    System.err.println("System.exit(0)");
    System.exit(0);
   }
   if (ans == 1){
     System.out.print("Enter the new port: ");
     port = in.nextInt();
     ans = 0;
   }
   if (ans == 0){
    System.out.print("Enter the IP: ");
    CS1 = new Socket(in.next(), port); //Creating an instane of the "CS1" socket
    System.err.print("Request sent on port " + port + ".");
    DOS1 = new DataOutputStream(CS1.getOutputStream());
    DIS1 = new DataInputStream(CS1.getInputStream()); //creating output and input streams
    System.err.println("Instances created!");
    if (DIS1 != null){
     System.err.println("DIS1 is connected!");
    }
    else {
     System.err.println("DIS1 is null!");
    }
    if (DOS1 != null) {
     System.err.println("DOS1 is connected!");
     System.out.print("Enter input: ");
     String FirstMessage = in.nextLine();
     DOS1.writeBytes(FirstMessage);
     String SecondMessage = DIS1.readLine();
     System.out.println(SecondMessage);
     if (SecondMessage.equals(FirstMessage)) {
     }
     else {
      System.out.println("Please check the code, FirstMessage != SecondMessage");
      }
    }
    else {
     System.err.println("DOS1 is null!");
    }
    if (CS1 != null) {
     System.err.println("CS1 is connected!");
    }
    else {
     System.err.println("CS1 is null!");
    }
    DIS1.close();
    DOS1.close();
    CS1.close(); //closing everything
   }
  }
  catch (IOException e){
   System.err.println("IOException: " + e.getMessage());
  }
 }
}

服务器代码:

import java.net.*;
import java.io.*;
import java.util.*;
public class TCPServer {
 /*Connecting to client
  */
 public static void Server (String[] args) {
  System.err.print("Starting up...");
  ServerSocket SS1 = null;
  DataOutputStream DOS1 = null;
  DataInputStream DIS1 = null;//Setting the values to null
  System.err.println("All created.");
  Scanner in = new Scanner(System.in);
  try {
   SS1 = new ServerSocket(47361); //setting the socket SS1 to port 5000 and creating an instance
   System.err.print("Listening on port 47361...");
   Socket CS1 = SS1.accept(); //accepting the connection request
   DOS1 = new DataOutputStream(CS1.getOutputStream());
   DIS1 = new DataInputStream(CS1.getInputStream());//creating output and input streams
   System.err.println("Instances created!");
   if (DIS1 != null){
    System.err.println("DIS1 is connected!");
    System.err.println(DIS1);
   }
   else {
    System.err.println("DIS1 is null!");
   }
   if (DOS1 != null) {
    System.err.println("DOS1 is connected!");
    System.err.println(DOS1);
   }
   else {
    System.err.println("DOS1 is null!");
   }
   if (SS1 != null) {
    System.err.println("SS1 is connected!");
    System.err.println(SS1);
   }
   else {
    System.err.println("SS1 is null!");
   }
   String FirstMessage = null;
   FirstMessage = DIS1.readLine();
   System.out.println(FirstMessage);
   String SecondMessage = FirstMessage;
   DOS1.writeBytes(SecondMessage);
   DIS1.close();
   DOS1.close();
   SS1.close(); //closing everything
  }
  catch (IOException error){
   System.err.println("IOException " + error.getMessage());
  }
catch (java.util.InputMismatchException error2){
   System.err.println("IOException " + error2.getMessage());
  }
 }
}

到目前为止,它几乎是一个 EchoServer,没有缓冲.我查看了调试器,它卡在服务器中的 FirstMessage = DIS1.readLine(); 行和 String SecondMessage = DIS1.readLine(); 行上客户.此外,即使我输入了任何可能的输入,服务器和客户端都在等待输入.为什么会这样?我怎样才能做到这一点?

It's pretty much an EchoServer so far, without Buffered. I looked in the debugger and it gets stuck on the FirstMessage = DIS1.readLine(); line in the server and the String SecondMessage = DIS1.readLine(); in the client. Also, both the server and client are waiting for input even after I've entered any possible input. Why is this happenenig? And how can I make this work?

旁注:我知道 i/o 流或套接字永远不会等于 null.此外,编译器警告我 java.io.DataInputStream 类已被弃用.您还推荐使用哪些其他课程?

A side note: I know that an i/o stream or a socket will not, ever, equal null. Also, the compiler is warning me that the class java.io.DataInputStream has been deprecated. What other classes do you recommend using?

另一边注:我是 IO 的新手,请不要因此而杀了我哈哈 :)

Another side note: I am new to IO, please don't kill me about it haha :)

谢谢!

推荐答案

常见问题.你在读台词,但不是在写台词.写入时为消息添加行终止符.

Usual problem. You're reading lines but you're not writing lines. Add a line terminator to the message when writing.

这篇关于从套接字java读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆