我正在尝试编写一个小聊天控制台,因为我最近学会了使用ServerSocket
和套接字。但我有一个问题,我没有接收/发送任何消息我做错了什么?
课程:聊天(主要)
package chatting;
import java.util.Scanner;
public class Chatting {
public static Client client;
public static Server server;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Your username: ");
String name = sc.nextLine();
System.out.print("connect to: ");
String ip = sc.nextLine();
client = new Client(ip.split(":")[0], Integer.parseInt(ip.split(":")[1]), name);
while (true){
String chat = sc.nextLine();
client.send(chat);
}
}
}
类:客户端
package chatting;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Client {
Socket s;
public Client(String ip, int port, String name){
if (ip.equalsIgnoreCase("localhost")){
Chatting.server = new Server();
}
new Thread(new Runnable(){
@Override
public void run(){
try{
s = new Socket(ip, port);
System.out.println("CONNECTION >> Waiting for conformation with name "+name+"!");
send("#reg"+name);
WaitForMessage("#confirmed");
System.out.println("CONNECTION >> You have been registered to server!");
while (true){
List<String> recieve = recieve();
if (!recieve.isEmpty()){
for (String str : recieve){
System.out.println(str);
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}).start();
}
public void send(String msg){
try{
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os), true);
pw.write(msg);
pw.flush();
}catch(Exception e){
e.printStackTrace();
}
}
public List<String> recieve(){
List<String> r = new ArrayList<String>();
try{
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine())!=null){
System.out.println("CRECIEVE >> "+line);
r.add(line);
}
}catch(Exception e){
e.printStackTrace();
}
return r;
}
public void WaitForMessage(String msg){
while (true){
if (recieve().contains(msg)) break;
}
}
}
类:服务器
package chatting;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Server {
ServerSocket ss;
List<Socket> s;
HashMap<String, String> registered;
public Server(){
s = new ArrayList<Socket>();
registered = new HashMap<String, String>();
try {
ss = new ServerSocket(1234);
System.out.println("SERVER >> created server on port 1234!");
} catch (IOException e) {
e.printStackTrace();
}
new Thread(new Runnable(){
@Override
public void run(){
while (true){
try{
Socket socket = ss.accept();
if (socket != null){
System.out.println("SERVER >> client connected "+socket.getLocalAddress().toString());
s.add(socket);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable(){
@Override
public void run(){
while (true){
for (Socket s : s){
List<String> result = translate(recieve(s));
if (!result.isEmpty()){
for (String str : result){
if (str.startsWith("#reg")){
str = str.replaceAll("#reg", "");
registered.put(s.getInetAddress().toString(), str);
System.out.println("SERVER >> user "+str+" on "+s.getLocalAddress().toString()+" registered!");
send(s, "#confirmed");
}else{
sendToAll(registered.get(s.getLocalAddress().toString())+" >> "+str);
}
}
}
}
}
}
}).start();
}
public void send(Socket s, String str){
try{
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os), true);
pw.write(str);
pw.flush();
}catch(Exception e){
e.printStackTrace();
}
}
public List<String> translate(String[] str){
List<String> r = new ArrayList<String>();
for (String s : str){
r.add(s);
}
return r;
}
public String[] recieve(Socket s){
String[] r = null;
try{
ArrayList<String> lines = new ArrayList<String>();
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println("SRECIEVE >> "+line);
lines.add(line);
}
r = new String[lines.size()];
for (int i = 0; i < lines.size(); i++){
r[i] = lines.get(i);
}
}catch(Exception e){
e.printStackTrace();
}
return r;
}
public void sendToAll(String str){
for (Socket soc : s){
send(soc, str);
}
}
我的问题是没有收到/发送消息。 当我启动服务器时,它启动客户端并完美连接,但是当我到达
时try{
s = new Socket(ip, port);
System.out.println("CONNECTION >> Waiting for conformation with name "+name+"!");
send("#reg"+name);
WaitForMessage("#confirmed");
System.out.println("CONNECTION >> You have been registered to server!");
while (true){
List<String> recieve = recieve();
if (!recieve.isEmpty()){
for (String str : recieve){
System.out.println(str);
}
}
}
}catch(Exception e){
e.printStackTrace();
}
没有任何事情发生了。
答案 0 :(得分:0)
你正在读行,但你不是在写行。使用println()
代替write()
。并修复@PeterLawrey提到的所有内容。如果readLine()
返回null,请关闭套接字并停止读取。