简体   繁体   English

通过套接字发送字符串

[英]Sending Strings through Sockets

Here is the Server 这是服务器

public class SocketMsg {

    public static void main(String[] args) throws IOException{
    ServerSocket ss = new ServerSocket("number goes here");


    System.out.println("Server Ready");
    ss.accept();


    }
    }

Client: 客户:

public class SocketMesg {



public static void main(String[] args) throws IOException{
  Socket socket = null;
    OutputStreamWriter osw;
    String str = "Hello World";
    try {
        socket = new Socket("localhost", "number goes here");
        osw =new OutputStreamWriter(socket.getOutputStream());
        osw.write(str, 0, str.length());
    } catch (IOException e) {
        System.err.print(e);
    } 
    finally {
        socket.close();
    }

}

Personally, the code works but the strings are not sending to the other host, I gave them the same number, but it is not working. 就个人而言,代码可以工作,但是字符串没有发送到其他主机,我给了他们相同的号码,但是它不起作用。 The client is sending it back to the server on the DOS window. 客户端将其发送回DOS窗口上的服务器。 Did I make a error? 我做错了吗? What did I do wrong? 我做错了什么?

Your server-side at least needs the following. 您的服务器端至少需要以下内容。

Socket clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));


String inputLine;
while ((inputLine = in.readLine()) != null) {
    // process inputLine;
}

You need to flush outputstream to commit write buffer to the socket. 您需要刷新输出流以将写缓冲区提交到套接字。 Also be careful with charset if writing strings. 如果编写字符串,也要注意字符集。 This example explicitly uses UTF-8 through "low level" byte array buffer. 本示例通过“低级”字节数组缓冲区显式使用UTF-8。 I think you are practicing your first socket programming so I kept this very simple. 我认为您正在练习您的第一个套接字编程,因此我保持了非常简单的状态。

Server.java 服务器.java

import java.net.*;
import java.io.*;

public class Server {

    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(1122);
        System.out.println("Server Ready");
        while(true) {
            Socket socket = ss.accept();
            InputStream is = socket.getInputStream();
            // client must send 1-10 bytes, no more in single command
            byte[] buf = new byte[10]; 
            int read = is.read(buf, 0, buf.length);
            String cmd = new String(buf, 0, read, "UTF-8");
            System.out.println(cmd);
            socket.close(); // we are done, this example reads input and then closes socket
        }
    }

}

Client.java 客户端.java

import java.net.*;
import java.io.*;

public class Client {

    public static void main(String[] args) throws Exception {
        Socket socket = null;
        // send max of 10 bytes to simplify this example
        String str = "ABCDEFGHIJ"; 
        try {
            socket = new Socket("localhost", 1122);
            OutputStream os = socket.getOutputStream();
            byte[] buf = str.getBytes("UTF-8");
            os.write(buf, 0, buf.length);
            os.flush();
        } catch (IOException ex) {
            System.err.print(ex);
        } finally {
            socket.close();
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM