Java实现点对点聊天
2013-05-26 11:19
120 查看
01.package socket;
02.
03.import java.net.*;
04.import java.io.*;
05.
06.public class ServerMain
07.{
08. public static void main(String args[])
09. {
10. try
11. {
12. boolean flag = true;
13. Socket clientSocket = null;
14. String inputLine;
15. int c;
16.
17. ServerSocket sSocket = new ServerSocket(8018);
18. System.out.println("Server listen on:" + sSocket.getLocalPort());
19. while(flag)
20. {
21. clientSocket = sSocket.accept();//接受客户端数据
22.
23.
24. DataInputStream is = new DataInputStream(
25. new BufferedInputStream(clientSocket.getInputStream()));
26.
27. OutputStream os = clientSocket.getOutputStream();//得到客户端的输出流
28.
29. while ((inputLine = is.readLine()) != null)
30. {
31. //当输入不为空-joh
32. // 当客户端输入stop时,程序将自行终止
33. if (inputLine.equals("stop"))
34. {
35. flag = false;
36. break;
37. }
38. else
39. {
40. System.out.println("Client Message is:"+inputLine);//显示输入
41. while ((c = System.in.read()) != -1)
42. {
43. os.write((byte) c);
44. if (c == '/n')
45. {
46. os.flush(); // 将信息发送到客户端
47. break;
48. }
49. }
50. }
51. }
52. is.close();
53. os.close();
54. clientSocket.close();
55. }
56. sSocket.close();
57. }
58. catch (Exception e)
59. {
60. System.out.println("Exception :" + e.getMessage());
61. }
62. }
63.}
客户端程序:
[c-sharp] view plaincopy
01.package client;
02.
03.import java.net.*;
04.import java.io.*;
05.import java.lang.*;
06.
07.public class ClientMain
08.{
09. public static void main(String args[])
10. {
11. try
12. {
14. Socket cSocket = new Socket("10.148.53.188", 8018);
15. //打开流
16. OutputStream os = cSocket.getOutputStream();
17. DataInputStream is = new DataInputStream(cSocket.getInputStream());
18.
19. int c;
20. boolean flag = true;
21.
22. String responseline;
23.
24. while (flag)
25. {
26. // 从标准输入输出接收字符
27. while ((c = System.in.read()) != -1)
28. {
29. //把读入的数据写进去
30. os.write((byte) c);
31. if (c == '/n')
32. {
33. os.flush();
34. // 將程序阻塞,直到信息收到时才释放
35. responseline = is.readLine();
36. System.out.println("Server Message is:" + responseline);
37. }
38. }
39. }
40. os.close();
41. is.close();
42. cSocket.close();
43.
44. }
45. catch (Exception e)
46. {
47. System.out.println("Exception :" + e.getMessage());
48. }
49. }
50.}
02.
03.import java.net.*;
04.import java.io.*;
05.
06.public class ServerMain
07.{
08. public static void main(String args[])
09. {
10. try
11. {
12. boolean flag = true;
13. Socket clientSocket = null;
14. String inputLine;
15. int c;
16.
17. ServerSocket sSocket = new ServerSocket(8018);
18. System.out.println("Server listen on:" + sSocket.getLocalPort());
19. while(flag)
20. {
21. clientSocket = sSocket.accept();//接受客户端数据
22.
23.
24. DataInputStream is = new DataInputStream(
25. new BufferedInputStream(clientSocket.getInputStream()));
26.
27. OutputStream os = clientSocket.getOutputStream();//得到客户端的输出流
28.
29. while ((inputLine = is.readLine()) != null)
30. {
31. //当输入不为空-joh
32. // 当客户端输入stop时,程序将自行终止
33. if (inputLine.equals("stop"))
34. {
35. flag = false;
36. break;
37. }
38. else
39. {
40. System.out.println("Client Message is:"+inputLine);//显示输入
41. while ((c = System.in.read()) != -1)
42. {
43. os.write((byte) c);
44. if (c == '/n')
45. {
46. os.flush(); // 将信息发送到客户端
47. break;
48. }
49. }
50. }
51. }
52. is.close();
53. os.close();
54. clientSocket.close();
55. }
56. sSocket.close();
57. }
58. catch (Exception e)
59. {
60. System.out.println("Exception :" + e.getMessage());
61. }
62. }
63.}
客户端程序:
[c-sharp] view plaincopy
01.package client;
02.
03.import java.net.*;
04.import java.io.*;
05.import java.lang.*;
06.
07.public class ClientMain
08.{
09. public static void main(String args[])
10. {
11. try
12. {
14. Socket cSocket = new Socket("10.148.53.188", 8018);
15. //打开流
16. OutputStream os = cSocket.getOutputStream();
17. DataInputStream is = new DataInputStream(cSocket.getInputStream());
18.
19. int c;
20. boolean flag = true;
21.
22. String responseline;
23.
24. while (flag)
25. {
26. // 从标准输入输出接收字符
27. while ((c = System.in.read()) != -1)
28. {
29. //把读入的数据写进去
30. os.write((byte) c);
31. if (c == '/n')
32. {
33. os.flush();
34. // 將程序阻塞,直到信息收到时才释放
35. responseline = is.readLine();
36. System.out.println("Server Message is:" + responseline);
37. }
38. }
39. }
40. os.close();
41. is.close();
42. cSocket.close();
43.
44. }
45. catch (Exception e)
46. {
47. System.out.println("Exception :" + e.getMessage());
48. }
49. }
50.}
相关文章推荐
- Java基于UDP用Socket实现点对点聊天
- JAVA通过Swing和socket编程实现简易点对点聊天
- java WebSocket实现简单的聊天室(包括群发和点对点聊天)
- Java实现点对点聊天
- Java实现点对点聊天
- C#下用P2P技术实现点对点聊天
- websocket+ javaweb 实现浏览器聊天
- java实现简单聊天程序(集合,线程,I/O,网络编程)
- java学习之利用TCP实现的简单聊天示例代码
- Java使用socket实现两人聊天对话
- java控制台实现QQ聊天程序
- Java之Socket简单聊天实现(QQ续二)
- C#下用P2P技术实现点对点聊天
- java实现简单聊天软件
- java+rabbitMQ实现一对一聊天
- Java网络----Socket, 多线程实现内网多人聊天及一对一聊天
- java TCP/IP实现简单的多人聊天功能
- Java系列讲座二:Socket网络通信实现聊天软件项目讲解(UDP)
- TCP c/s模式实现点对点,一对多聊天 识别不同的TCP通道.给相应的TCP客户发送信息
- java+html5的websocket简单实现BS聊天DEMO实例