这里我创建了一个充当客户端的GUI。它包含一个文本字段和一个按钮。我的目的是打印文本字段中键入的用户必须在服务器命令提示符下显示的内容。
Server.java
import java.io.*;
import java.net.*;
public class Server {
Server(){
try{
ServerSocket server = new ServerSocket(4040);
while(true){
Socket sock = server.accept();
InputStream is = sock.getInputStream();
InputStreamReader streamReader = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(streamReader);
String string = reader.readLine();
System.out.println(string);
reader.close();
}
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
public static void main(String args[]){
new Server();
}
}
SimpleChatClientA.java
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.net.*;
import java.awt.*;
public class SimpleChatClientA {
JTextField outgoing;
PrintWriter writer;
Socket sock;
public void go(){
JFrame frame = new JFrame("chat client");
outgoing = new JTextField(20);
JButton sendButton = new JButton("send");
frame.add(outgoing);
frame.add(sendButton);
sendButton.addActionListener(new SendButtonListener());
frame.setLayout(new FlowLayout());
frame.setSize(500,100);
setUpNetworking();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setUpNetworking(){
try {
Socket sock = new Socket("localhost",4040);
PrintWriter writer = new PrintWriter(sock.getOutputStream());
System.out.println("connection established");
} catch (Exception e) {
e.printStackTrace();
}
}
public class SendButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try {
String str = outgoing.getText();
writer.println(str);
writer.flush();
}
catch (Exception ex) {
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}
}
public static void main(String args[]){
SimpleChatClientA simple = new SimpleChatClientA();
simple.go();
}
}
当我运行此程序时。连接建立。但是我无法看到在服务器命令promt中输入JTextField的消息。我看到发生了一些错误。我不明白它是什么。我是java的新手。请帮助我
答案 0 :(得分:1)
问题似乎是,你正在创建编写器并存储在局部变量中。
private void setUpNetworking(){
try {
Socket sock = new Socket("localhost",4040);
// PROBLEM IS HERE
PrintWriter writer = new PrintWriter(sock.getOutputStream());
System.out.println("connection established");
} catch (Exception e) {
e.printStackTrace();
}
}
试试这个:
private void setUpNetworking(){
try {
Socket sock = new Socket("localhost",4040);
// STORE WRITER IN INSTANCE VARIABLE
writer = new PrintWriter(sock.getOutputStream());
System.out.println("connection established");
} catch (Exception e) {
e.printStackTrace();
}
}
你必须有NullPointerException,因为你还没有创建writer Object。
public void actionPerformed(ActionEvent e) {
try {
String str = outgoing.getText();
// HERE YOU WILL GET EXCEPTION AS WRITER (instace variable) IS NOT CREATED
writer.println(str);
writer.flush();
}
catch (Exception ex) {
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}
答案 1 :(得分:1)
您可以像这样编写服务器以接受多行:
package com.stackoverflow.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
Server(){
try{
ServerSocket server = new ServerSocket(4040);
while(true){
Socket sock = server.accept();
SocketHandler handler = new SocketHandler(sock);
Thread t = new Thread(handler);
t.start();
}
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
public static class SocketHandler implements Runnable {
private final Socket socket;
private final BufferedReader reader;
public SocketHandler(Socket socket) throws IOException {
this.socket = socket;
this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
@Override
public void run() {
String message = null;
try {
while ((message = reader.readLine()) != null) {
System.out.println("incoming message: " + message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String args[]){
new Server();
}
}