所以我正在制作一个简单的聊天应用程序,以便在Java Networking中启动。我正在使用TCP / IP。问题是客户端不从服务器发送/接收数据,服务器也没有。我在哪里做错了? 这是我的服务器类:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server implements Runnable {
static InetAddress address;
static ArrayList<Integer> clients=new ArrayList<Integer>();
static ArrayList<Socket> s=new ArrayList<Socket>();
static String message;
static byte[] mess=new byte[2048];
static DataOutputStream toClient;
public Server(InetAddress address){
this.address=address;
}
public static void main(String[] args) throws IOException
{
ServerSocket socket=new ServerSocket(8000);
System.out.println("Server started on port 8000");
while(true)
{
Socket socketNew=socket.accept();
s.add(socketNew);
address=socketNew.getInetAddress();
System.out.println("connected to client at address: "+address);
InputStream is=socketNew.getInputStream(); //receiving from client
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
message=br.readLine();
Server server=new Server(address);
//new Thread(server).start();
}
}
public void run() {
for(Socket socket:s) //to send message to all clients
{
OutputStream os;
try {
os = socket.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os);
BufferedWriter bw=new BufferedWriter(osw);
bw.write(message);
bw.flush();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
这是我的客户端类'发送和接收函数:
private void send_data() {
try {
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os);
BufferedWriter bw=new BufferedWriter(osw);
String sendMessage=this.getName()+": "+textField.getText();
bw.write(sendMessage);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private void receive_data()
{
try {
InputStream is = socket.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String toConsole=br.readLine();
console(toConsole);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 0 :(得分:0)
我不知道您是如何编写客户端代码以连接到Server
的,我不知道您是否设置了ip-configuratoin
对或错。
通常,通过Socket
和ServerSocket
写一些内容很简单,就是在邮件末尾添加一个行分隔符,以使另一方从套接字readLine()
开始
这可以通过在邮件中附加\n
来实现。但我自己更喜欢使用PrintWriter
而不是BufferedWriter
,因为它有一组优雅的重载println
方法,可以自动为您执行此操作:
PrintWriter p = new PrintWriter(bw);
p.println(message);
希望这会有所帮助。
答案 1 :(得分:0)
您的服务器正在等待从客户端接收/ n终止的消息。
服务器代码:
package general;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class ChatServer implements Runnable {
static InetAddress address;
static ArrayList<Integer> clients=new ArrayList<Integer>();
static ArrayList<Socket> s=new ArrayList<Socket>();
static String message;
static byte[] mess=new byte[2048];
static DataOutputStream toClient;
public ChatServer(InetAddress address){
this.address=address;
}
public static void main(String[] args) throws IOException
{
ServerSocket socket=new ServerSocket(9000);
System.out.println("Server started on port 9000");
while(true)
{
Socket server=socket.accept();
s.add(server);
address=server.getInetAddress();
System.out.println("connected to client at address: "+address);
BufferedReader br = new BufferedReader(new InputStreamReader(server.getInputStream()));
String resp = br.readLine();
System.out.println("Server received from Client:" + resp);
server.close();
/*
DataInputStream in = new DataInputStream(socketNew.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out =
new DataOutputStream(socketNew.getOutputStream());
out.writeUTF("Thank you for connecting to "
+ socketNew.getLocalSocketAddress() + "\nGoodbye!");
*/
}
}
public void run() {
for(Socket socket:s) //to send message to all clients
{
OutputStream os;
try {
os = socket.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os);
BufferedWriter bw=new BufferedWriter(osw);
bw.write(message);
bw.flush();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户代码:
public static void main(String[] args) {
try {
client=new Socket("127.0.0.1",9000);
System.out.println("connected to: " + client.getRemoteSocketAddress());
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
String sendingMessage = "Hello Server - This is the client!\n";
bw.write(sendingMessage);
bw.flush();
System.out.println("Message sent to the server : "+ sendingMessage);
/*
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Hello from \n" + client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
*/
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
输出:
服务器: 服务器在端口9000上启动 连接到客户端地址:/127.0.0.1 从客户端收到的服务器:Hello Server - 这是客户端!
客户端: 连接到:/127.0.0.1:9000 发送到服务器的消息:Hello Server - 这是客户端!