编一个简单的java聊天程序,但是运行时服务器和客户端都出错
改聊天工具希望得到的界面如下:||A(IP:端口):。。。(数据)。。。(时间)||B(IP:端口):。。。(数据)。。。(时间)服务器代码如下:packageChatP...
改聊天工具希望得到的界面如下:
|| A(IP:端口):。。。(数据)。。。 (时间)
|| B(IP:端口):。。。(数据)。。。 (时间)
服务器代码如下:
package ChatProText;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
public class ChatServer {
private static int PORTNUM=1123;//定义程序使用的端口号PORTNUM
public static void main(String args[]) throws Exception
{
ServerSocket s=new ServerSocket(PORTNUM);
while(true)
{
Socket s1=s.accept();
BufferedReader InputS=new BufferedReader(new InputStreamReader(s1.getInputStream()));
System.out.println(InputS.readLine());
Date now=new Date();
DateFormat d=DateFormat.getDateTimeInstance();
String time=d.format(now);
PrintWriter OutputS=new PrintWriter(s1.getOutputStream());
BufferedReader say=new BufferedReader(new InputStreamReader(System.in));
String Ssay=say.readLine();
OutputS.println("Server"+String.valueOf(s1.getLocalPort())+Ssay+time);
OutputS.flush();
System.out.println("Server"+String.valueOf(s1.getLocalPort())+Ssay+time);
}
}
}
客户端代码如下:
package ChatProText;
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
public class ChatClient {
public static void main(String[] args)throws Exception
{
Socket c=new Socket("127.0.0.1",1123);
while(true)
{
BufferedReader InputS=new BufferedReader(new InputStreamReader(c.getInputStream()));
System.out.println(InputS.readLine());
Date now=new Date();
DateFormat d=DateFormat.getDateTimeInstance();
String time=d.format(now);
PrintWriter OutputS=new PrintWriter(c.getOutputStream());
BufferedReader say=new BufferedReader(new InputStreamReader(System.in));
String Csay=say.readLine();
OutputS.println("Client:"+String.valueOf(c.getLocalPort())+Csay+time);
OutputS.flush();
System.out.println("Client:"+String.valueOf(c.getLocalPort())+Csay+time);
}
}
}
编译没问题 可是运行时却总是报错·
求各位帮我调试一下·
帮我解决· 展开
|| A(IP:端口):。。。(数据)。。。 (时间)
|| B(IP:端口):。。。(数据)。。。 (时间)
服务器代码如下:
package ChatProText;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
public class ChatServer {
private static int PORTNUM=1123;//定义程序使用的端口号PORTNUM
public static void main(String args[]) throws Exception
{
ServerSocket s=new ServerSocket(PORTNUM);
while(true)
{
Socket s1=s.accept();
BufferedReader InputS=new BufferedReader(new InputStreamReader(s1.getInputStream()));
System.out.println(InputS.readLine());
Date now=new Date();
DateFormat d=DateFormat.getDateTimeInstance();
String time=d.format(now);
PrintWriter OutputS=new PrintWriter(s1.getOutputStream());
BufferedReader say=new BufferedReader(new InputStreamReader(System.in));
String Ssay=say.readLine();
OutputS.println("Server"+String.valueOf(s1.getLocalPort())+Ssay+time);
OutputS.flush();
System.out.println("Server"+String.valueOf(s1.getLocalPort())+Ssay+time);
}
}
}
客户端代码如下:
package ChatProText;
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
public class ChatClient {
public static void main(String[] args)throws Exception
{
Socket c=new Socket("127.0.0.1",1123);
while(true)
{
BufferedReader InputS=new BufferedReader(new InputStreamReader(c.getInputStream()));
System.out.println(InputS.readLine());
Date now=new Date();
DateFormat d=DateFormat.getDateTimeInstance();
String time=d.format(now);
PrintWriter OutputS=new PrintWriter(c.getOutputStream());
BufferedReader say=new BufferedReader(new InputStreamReader(System.in));
String Csay=say.readLine();
OutputS.println("Client:"+String.valueOf(c.getLocalPort())+Csay+time);
OutputS.flush();
System.out.println("Client:"+String.valueOf(c.getLocalPort())+Csay+time);
}
}
}
编译没问题 可是运行时却总是报错·
求各位帮我调试一下·
帮我解决· 展开
展开全部
给你简单修改了一下,你看看吧
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
public class ChatClient {
public static void main(String[] args) throws Exception {
Socket c = new Socket("127.0.0.1", 1123);
while (true) {
Date now = new Date();
DateFormat d = DateFormat.getDateTimeInstance();
String time = d.format(now);
PrintWriter OutputS = new PrintWriter(c.getOutputStream());
BufferedReader say = new BufferedReader(new InputStreamReader(System.in));
String Csay = say.readLine();
System.out.println("Client:" + String.valueOf(c.getLocalPort()) + Csay + time);
OutputS.println("Client:" + String.valueOf(c.getLocalPort()) + Csay + time);
OutputS.flush();
BufferedReader InputS = new BufferedReader(new InputStreamReader(c.getInputStream()));
System.out.println(InputS.readLine());
}
}
}
、、、、、、、、、、、、、、、、
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
public class ChatServer {
private static int PORTNUM = 1123;// 定义程序使用的端口号PORTNUM
public static void main(String args[]) throws Exception {
ServerSocket s = new ServerSocket(PORTNUM);
while (true) {
Socket s1 = s.accept();
BufferedReader InputS = new BufferedReader(new InputStreamReader(s1.getInputStream()));
System.err.println();
System.out.println(InputS.readLine());
Date now = new Date();
DateFormat d = DateFormat.getDateTimeInstance();
String time = d.format(now);
PrintWriter OutputS = new PrintWriter(s1.getOutputStream());
BufferedReader say = new BufferedReader(new InputStreamReader(System.in));
String Ssay = say.readLine();
OutputS.println("Server" + String.valueOf(s1.getLocalPort()) + Ssay + time);
OutputS.flush();
System.out.println("Server" + String.valueOf(s1.getLocalPort()) + Ssay + time);
}
}
}
说一下你的程序出现的原因吧!
你的客户端与程序段在开始运行都在监听对方发来的消息,当什么都不会有了,因为对方都在等着接受消息呢,这样就造成了死锁
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
public class ChatClient {
public static void main(String[] args) throws Exception {
Socket c = new Socket("127.0.0.1", 1123);
while (true) {
Date now = new Date();
DateFormat d = DateFormat.getDateTimeInstance();
String time = d.format(now);
PrintWriter OutputS = new PrintWriter(c.getOutputStream());
BufferedReader say = new BufferedReader(new InputStreamReader(System.in));
String Csay = say.readLine();
System.out.println("Client:" + String.valueOf(c.getLocalPort()) + Csay + time);
OutputS.println("Client:" + String.valueOf(c.getLocalPort()) + Csay + time);
OutputS.flush();
BufferedReader InputS = new BufferedReader(new InputStreamReader(c.getInputStream()));
System.out.println(InputS.readLine());
}
}
}
、、、、、、、、、、、、、、、、
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
public class ChatServer {
private static int PORTNUM = 1123;// 定义程序使用的端口号PORTNUM
public static void main(String args[]) throws Exception {
ServerSocket s = new ServerSocket(PORTNUM);
while (true) {
Socket s1 = s.accept();
BufferedReader InputS = new BufferedReader(new InputStreamReader(s1.getInputStream()));
System.err.println();
System.out.println(InputS.readLine());
Date now = new Date();
DateFormat d = DateFormat.getDateTimeInstance();
String time = d.format(now);
PrintWriter OutputS = new PrintWriter(s1.getOutputStream());
BufferedReader say = new BufferedReader(new InputStreamReader(System.in));
String Ssay = say.readLine();
OutputS.println("Server" + String.valueOf(s1.getLocalPort()) + Ssay + time);
OutputS.flush();
System.out.println("Server" + String.valueOf(s1.getLocalPort()) + Ssay + time);
}
}
}
说一下你的程序出现的原因吧!
你的客户端与程序段在开始运行都在监听对方发来的消息,当什么都不会有了,因为对方都在等着接受消息呢,这样就造成了死锁
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询