Java 插座未连接
我的问题如下:我有以下客户端类:Java 插座未连接,java,sockets,networking,network-programming,nullpointerexception,Java,Sockets,Networking,Network Programming,Nullpointerexception,我的问题如下:我有以下客户端类: import java.io.IOException; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class Client { public final int portNumber = 6
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
public final int portNumber = 6040;
public Socket socket;
private PrintWriter pw;
/**
* @param args
* @throws IOException
*/
public void connect() throws IOException{
// du kan vælge at bruge inetadressen til at connecte i socketet.
InetAddress adr = InetAddress.getByName("localhost");
socket = new Socket("localhost", portNumber);
pw = new PrintWriter(socket.getOutputStream());
pw.println("Connected waiting for input");
pw.flush();
}
/**
* This method sends the message (that the client(chat person) writes to the user)
* @param x
* @throws NullPointerException
* @throws IOException
*/
public void SendChat(String x) throws NullPointerException{
pw.print(x);
pw.flush();
}
public int sendCommando(int id) throws IOException{
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.print(id);
/*
* this part of the program sends a command to the server if the command is 1 then 1 is = Connect.
* the program then ask the server is the server is full or is it ok to connect?
* if the response is not 10 then the program will allow a connection to happen the return type will be the Id of which
* the chat person becomes!
*/
// should the method return 0 the Application will do NOTHING!
switch (id) {
case 1:
int k = reciveCommando();
if (k== 10) {
return 10;
}else if (k<= 3) {
return k;
}else {
return 10;
}
/*
* Closes the connection with the server!
*/
case 3:
socket.close();
return 0;
default:
return 0;
}
}
/*
* this method recives a command from the server! the comands can be found in the ChatCommands.txt
* returns the command as an integer!
*/
public int reciveCommando() throws IOException{
Scanner commandoFromServer = new Scanner(socket.getInputStream());
Integer i = Integer.parseInt(commandoFromServer.nextLine());
return i;
}
/**
* Gets a String response from the server. This method i used to create other users and give them the correct username.
*
* @param i
* @return
* @throws IOException
*/
public String getStringResponse(int i) throws IOException {
pw.print(i);
pw.flush();
Scanner commandFromServer = new Scanner(socket.getInputStream());
String x = commandFromServer.nextLine();
return x;
}
}
import java.io.IOException;
导入java.io.PrintWriter;
导入java.net.InetAddress;
导入java.net.Socket;
导入java.net.UnknownHostException;
导入java.util.Scanner;
公共类客户端{
公共最终int端口号=6040;
公共插座;
私人印刷作家;
/**
*@param args
*@抛出异常
*/
public void connect()引发IOException{
//在bruge inetadressen直到连接到插座时,都可以使用vælge。
InetAddress adr=InetAddress.getByName(“本地主机”);
套接字=新套接字(“本地主机”,端口号);
pw=新的PrintWriter(socket.getOutputStream());
println(“连接等待输入”);
pw.flush();
}
/**
*此方法发送消息(客户端(聊天室人员)向用户写入的消息)
*@param x
*@抛出NullPointerException
*@抛出异常
*/
public void SendChat(字符串x)引发NullPointerException{
印刷品(x);
pw.flush();
}
公共int发送命令(int id)引发IOException{
PrintWriter pw=新的PrintWriter(socket.getOutputStream());
印刷品(id);
/*
*程序的这一部分向服务器发送命令,如果命令为1,则1为=连接。
*然后程序会询问服务器服务器是否已满,或者是否可以连接?
*如果响应不是10,则程序将允许连接发生。返回类型将是
*聊天的人变成!
*/
//如果该方法返回0,应用程序将不执行任何操作!
开关(id){
案例1:
int k=reciveCommando();
如果(k==10){
返回10;
}否则,如果(k似乎很奇怪,“sendCommandO”创建了自己的printwriter,而不是使用
全球一号。以后再找两个印刷作家吃饭肯定是自找麻烦
同样的流。“连接”立即发送“已连接…”似乎也很奇怪
一般来说,此程序的这种结构不起作用,因为在读卡器读取某些内容之前,您的写入将被阻止,并且无法解除阻止。对于少量数据,它似乎可以正常工作。读取和写入必须在单独的线程中完成
这些都不是您当前的问题,但我建议您完全重写。在套接字的生命周期中,您必须使用相同的流、扫描仪等。不要在每次需要进行I/O时都创建新的流、扫描仪等。否则您将丢失缓冲区中的数据。首先感谢大家的帮助和响应这就是我的代码工作的原因:
清理了代码,以确保每个代码只创建了1个,并且这些代码是在客户端连接到服务器时创建的
为了确保我可以在所有方法中同时使用PrintWriter和Scanner,我必须将字段设置为静态
感谢您的所有帮助我的客户端现在连接到服务器并能够来回发送消息!:)
完整代码如下:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public final static int portNumber = 6040;
public static Socket socket;
private static PrintWriter pw;
private static Scanner input;
/**
* @param args
* @throws IOException
*/
public static void connect() throws IOException{
// du kan vælge at bruge inetadressen til at connecte i socketet.
InetAddress adr = InetAddress.getByName("localhost");
socket = new Socket("localhost", portNumber);
input=new Scanner(socket.getInputStream());
pw = new PrintWriter(socket.getOutputStream());
pw.println("Connected waiting for input");
pw.flush();
}
/**
* This method sends the message (that the client(chat person) writes to the user)
* @param x
* @throws NullPointerException
* @throws IOException
*/
public void SendChat(String x) throws NullPointerException{
pw.print(x);
pw.flush();
}
public int sendCommando(int id) throws IOException{
pw.print(id);
pw.flush();
/*
* this part of the program sends a command to the server if the command is 1 then 1 is = Connect.
* the program then ask the server is the server is full or is it ok to connect?
* if the response is not 10 then the program will allow a connection to happen the return type will be the Id of which
* the chat person becomes!
*/
// should the method return 0 the Application will do NOTHING!
switch (id) {
case 1:
int k = reciveCommando();
if (k== 10) {
return 10;
}else if (k < 3) {
System.out.println("returned k" + k);
return k;
}else {
return 10;
}
/*
* Closes the connection with the server!
*/
case 3:
socket.close();
return 0;
default:
return 0;
}
}
/*
* this method recives a command from the server! the comands can be found in the ChatCommands.txt
* returns the command as an integer!
*/
public int reciveCommando() throws IOException{
Integer i = input.nextInt();
return i;
}
/**
* Gets a String response from the server. This method i used to create other users and give them the correct username.
*
* @param i
* @return
* @throws IOException
*/
public String getStringResponse(int i) throws IOException {
pw.print(i);
pw.flush();
String x = input.nextLine();
return x;
}
}
import java.io.IOException;
导入java.io.PrintWriter;
导入java.net.InetAddress;
导入java.net.Socket;
导入java.util.Scanner;
公共类客户端{
公共最终静态整数端口号=6040;
公共静电插座;
专用静态打印机;
专用静态扫描仪输入;
/**
*@param args
*@抛出异常
*/
公共静态void connect()引发IOException{
//在bruge inetadressen直到连接到插座时,都可以使用vælge。
InetAddress adr=InetAddress.getByName(“本地主机”);
套接字=新套接字(“本地主机”,端口号);
输入=新扫描仪(socket.getInputStream());
pw=新的PrintWriter(socket.getOutputStream());
println(“连接等待输入”);
pw.flush();
}
/**
*此方法发送消息(客户端(聊天室人员)向用户写入的消息)
*@param x
*@抛出NullPointerException
*@抛出异常
*/
public void SendChat(字符串x)引发NullPointerException{
印刷品(x);
pw.flush();
}
公共int发送命令(int id)引发IOException{
印刷品(id);
pw.flush();
/*
*程序的这一部分向服务器发送命令,如果命令为1,则1为=连接。
*然后程序会询问服务器服务器是否已满,或者是否可以连接?
*如果响应不是10,则程序将允许连接发生。返回类型将是
*聊天的人变成!
*/
//如果该方法返回0,应用程序将不执行任何操作!
开关(id){
案例1:
int k=reciveCommando();
如果(k==10){
返回10;
}else if(k<3){
System.out.println(“返回的k”+k);
返回k;
}否则{
返回10;
}
/*
*关闭与服务器的连接!
*/
案例3:
socket.close();
返回0;
违约:
返回0;
}
}
/*
*此方法接收来自服务器的命令!命令可以在chatcomands.txt中找到
*以整数形式返回命令!
*/
public int reciveCommando()引发IOException{
整数i=input.nextInt();
返回i;
}
/**
*从服务器获取字符串响应。此方法用于创建其他用户并为他们提供正确的用户名。
*
*@param i
*@返回
*@抛出异常
*/
公共字符串getStringResponse(int i)引发IOException{
印刷品(一);
pw.flush();
字符串x=input.nextLine();
返回