我有一个简单的服务器和客户端代码,我一直在创建。我是新来的网络,我已经被困在这个错误的绝对年龄。我知道消息就在那里,因为我可以将消息输出到控制台。我不知道为什么这不工作,我会链接服务器和客户端的代码和下面的错误。非常感谢。
服务器代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package oserver;
import java.net.*;
import java.io.*;
import com.dosse.upnp.UPnP;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
/**
*
* @author sgmud
*/
public class SServer { // Server Class
public ServerSocket SSocket;
public Socket CSocket;
public PrintWriter out;
public BufferedReader in;
public int GetPort(){ // Gets port number for socket to be set listening to
Scanner scan = new Scanner(System.in);
System.out.println("please enter the port number");
int PortNum = scan.nextInt();
return PortNum;
}
public void start(int port) { // Starts the server with the collected port
try{
System.out.println("Server Started");
UPnP.openPortTCP(port);
SSocket = new ServerSocket(port);
CSocket = SSocket.accept();
System.out.println("Server Connected");
out = new PrintWriter(CSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(CSocket.getInputStream()));
Timer timer = new Timer();
int seconds;
seconds = 1;
timer.scheduleAtFixedRate(
new TimerTask()
{
public void run()
{
Listner();
}
},
0, // run first occurrence immediately
1000); // run every 1 seconds
SServer server = new SServer();
server.convosation();
/**
String input;
while ((input = in.readLine()) != null){
if(".".equals(input)){
out.println("goodbye");
break;
}
else{
out.println(input);
}
}
**/
}catch(IOException e){
System.out.println("ERROR");
}
}
public String Message(){
Scanner scan = new Scanner(System.in);
System.out.println("please enter a mesasge to be sent");
String message = scan.nextLine();
return message;
}
public void convosation(){ // method will keep letting you send a message untill you stop
SServer client = new SServer();
while (true){
Scanner scan = new Scanner(System.in);
System.out.println("Type QUIT to end the convosation or press any key to send a message");
String qit = scan.nextLine();
if("QUIT".equals(qit)){
client.STOP();
}
else{
client.sendMessage(client.Message()); // Runs the send message method with the output from the Message method
}
}
}
public void stop(){ // Will close all connections
try{
//in.close();
out.close();
CSocket.close();
SSocket.close();
}catch(IOException e){
System.out.println("ERROR");
}
}
public void sendMessage(String msg){
if(msg.equals("null")){
SServer client = new SServer();
client.sendMessage(client.Message());// outputs message
}
else{
out.println(msg);
}
//try{
// Response = in.readLine();
//}catch(IOException e){
// System.out.println("ERROR");
//}
//System.out.println(Response);
//return Response;
} // outputs message
public static void main(String[] args){
SServer server = new SServer(); // Creat new server class
server.start(server.GetPort()); // Starts the server with the port number
}
public void STOP(){
try{
in.close();
out.close();
CSocket.close();
}catch(IOException e){
System.out.println("ERROR");
}
}
public void Listner() {
String message = "";
try{
message = in.readLine();
}catch(IOException e){
}
if(message.equals("")==false){System.out.println(message);}
}
}
客户代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package oclient;
import java.net.*;
import java.io.*;
import com.dosse.upnp.UPnP;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
/**
*
* @author sgmud
*/
public class CClient {
public Socket CSocket;
public PrintWriter output;
public BufferedReader in;
public int GetPort(){
Scanner scan = new Scanner(System.in);
System.out.println("please enter the port number");
int PortNum = scan.nextInt();
return PortNum;
}
public String GetAddress(){
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the IP address of the host");
String Address = scan.nextLine();
return Address;
}
public void StartConnection(String ip, int port){
try{
CSocket = new Socket(ip, port);
output = new PrintWriter(CSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(CSocket.getInputStream()));
Timer timer = new Timer();
int seconds;
seconds = 1;
timer.scheduleAtFixedRate(
new TimerTask()
{
public void run()
{
Listner();
}
},
0, // run first occurrence immediately
1000); // run every 1 seconds
CClient server = new CClient();
}catch(IOException e){
System.out.println("ERROR");
}
}
public void sendMessage(String msg){
if(msg.equals("null")){
CClient client = new CClient();
client.sendMessage(client.Message());// outputs message
}
else{
System.out.println(msg);
output.println(msg);
}
//try{
// Response = in.readLine();
//}catch(IOException e){
// System.out.println("ERROR");
//}
//System.out.println(Response);
//return Response;
} // outputs message
public String receveMessage(){
String Response = "null";
try{
Response = in.readLine();
}catch(IOException e){
System.out.println("Error");
return Response;
}
System.out.println(Response);
return Response;
}
public void convosation(){ // method will keep letting you send a message untill you stop
CClient client = new CClient();
while (true){
Scanner scan = new Scanner(System.in);
System.out.println("Type QUIT to end the convosation or press any key to send a message");
String qit = scan.nextLine();
if("QUIT".equals(qit)){
client.STOP();
}
else{
client.sendMessage(client.Message()); // Runs the send message method with the output from the Message method
}
}
}
public String Message(){
Scanner scan = new Scanner(System.in);
System.out.println("please enter a mesasge to be sent");
String message = scan.nextLine();
return message;
}
public void STOP(){
try{
in.close();
output.close();
CSocket.close();
}catch(IOException e){
System.out.println("ERROR");
}
}
public void Listner() {
String message = "";
try{
message = in.readLine();
}catch(IOException e){
}
if(message.equals("")==false){System.out.println(message);}
}
/**
*
* @param args
*/
public static void main(String[] args){
CClient client = new CClient(); // Making a new client class
client.StartConnection(client.GetAddress(), client.GetPort()); // runs the startConnection method but runs the Get address and Get port method first so the Start connection method has the IP and Port number
client.convosation();
// client.STOP(); // runs the stop method which will terminate the server
}
}
错误:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.PrintWriter.println(String)" because "this.output" is null
at oclient.CClient.sendMessage(CClient.java:79)
at oclient.CClient.convosation(CClient.java:122)
at oclient.CClient.main(CClient.java:169)
抱歉,如果我有点吹牛,我真的不明白为什么这是不工作。也请忽略我糟糕的错误处理,我计划在以后的工作。任何帮助都是值得的。:)
暂无答案!
目前还没有任何答案,快来回答吧!