中易网

现在要做及时通讯用java怎么做哇?各位大牛,小弟多谢了!

答案:1  悬赏:80  
解决时间 2021-03-07 09:21
现在要做及时通讯用java怎么做哇?各位大牛,小弟多谢了!
最佳答案
下面是一个java的多线程的WebServer的例子:

//import java.io.*;
import java.net.*;
//import java.util.*;

public final class WebServer {
 public static void main(String argv[]) throws Exception
 {
 int port = 80;
 // Establish the listen socket.
 ServerSocket WebSocket = new ServerSocket(port);
 while (true) {
 // Listen for a TCP connection request.
 Socket connectionSocket = WebSocket.accept();
 //Construct object to process HTTP request message
 HttpRequest request = new HttpRequest(connectionSocket);

 Thread thread = new Thread(request); //Create new thread to process

 thread.start(); //Start the thread

 }
 }
}

import java.io.*;
import java.net.*;
import java.util.*;

public final class HttpRequest implements Runnable {

 final static String CRLF = "
";//For convenience
 Socket socket;

 // Constructor
 public HttpRequest(Socket socket) throws Exception
 {
 this.socket = socket;
 }

 // Implement the run() method of the Runnable interface.
 public void run()
 {
 try {
 processRequest();
 } catch (Exception e) {
 System.out.println(e);
 }
 }

 private void processRequest() throws Exception
 {
 InputStream is = socket.getInputStream(); //Starts the input from client machine

 DataOutputStream os = new DataOutputStream(
 socket.getOutputStream());

 // Set up input stream filters.

 BufferedReader br = new BufferedReader(
 new InputStreamReader(is));

 String requestLine = br.readLine();

 System.out.println();  //Echoes request line out to screen
 System.out.println(requestLine);

 //The following obtains the IP address of the incoming connection.

 InetAddress incomingAddress = socket.getInetAddress();
 String ipString= incomingAddress.getHostAddress();
 System.out.println("The incoming address is:   " + ipString);

 //String Tokenizer is used to extract file name from this class.
 StringTokenizer tokens = new StringTokenizer(requestLine);
 tokens.nextToken();  // skip over the method, which should be “GET”
 String fileName = tokens.nextToken();
 // Prepend a “.” so that file request is within the current directory.
 fileName = "." + fileName;

 String headerLine = null;
 while ((headerLine = br.readLine()).length() != 0) { //While the header still has text, print it
 System.out.println(headerLine);
 }

 // Open the requested file.
 FileInputStream fis = null;
 boolean fileExists = true;
 try {
 fis = new FileInputStream(fileName);
 } catch (FileNotFoundException e) {
 fileExists = false;
 }   

 //Construct the response message
 String statusLine = null; //Set initial values to null
 String contentTypeLine = null;
 String entityBody = null;
 if (fileExists) {
 statusLine = "HTTP/1.1 200 OK: ";
 contentTypeLine = "Content-Type: " +
 contentType(fileName) + CRLF;
 } else {
 statusLine = "HTTP/1.1 404 Not Found: ";
 contentTypeLine = "Content-Type: text/html" + CRLF;
 entityBody = "" + "Not Found" + "Not Found";
 }
 //End of response message construction

 // Send the status line.
 os.writeBytes(statusLine);

 // Send the content type line.
 os.writeBytes(contentTypeLine);

 // Send a blank line to indicate the end of the header lines.
 os.writeBytes(CRLF);

 // Send the entity body.
 if (fileExists) {
 sendBytes(fis, os);
 fis.close();
 } else {
 os.writeBytes(entityBody);
 }

 os.close(); //Close streams and socket.
 br.close();
 socket.close();

 }

//Need this one for sendBytes function called in processRequest
private static void sendBytes(FileInputStream fis, OutputStream os)
throws Exception
{
 // Construct a 1K buffer to hold bytes on their way to the socket.
 byte[] buffer = new byte[1024];
 int bytes = 0;

 // Copy requested file into the socket’s output stream.
 while((bytes = fis.read(buffer)) != -1 ) {
 os.write(buffer, 0, bytes);
 }
}
private static String contentType(String fileName)
 {
 if(fileName.endsWith(".htm") || fileName.endsWith(".html"))
 return "text/html";
 if(fileName.endsWith(".jpg"))
 return "text/jpg";
 if(fileName.endsWith(".gif"))
 return "text/gif";
 return "application/octet-stream";
 }
}追问需求上说还要用xmpp协议,我就在看openfire。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
龙须兰叶心开始发黄变烂,这是什么原因导致的
大武汉1911北门这个地址在什么地方,我要处理
请问下面两个概率论里有关随机事件的公式哪个
小学数学倒数中0.25和4的乘积是1,那么0.25和
奔驰C180发动机锁住油门踩没反应
手瘦的人带长方形的手表好看还是圆形的好看
区哪个印刷公司比较好
在mysql中对时间排序并且对用户去重的sql 该
下雨天两个女孩的对话作文
我的八哥从小就买回来的,开始一直都是放在盒
鑫艺木雕在哪里啊,我有事要去这个地方
有没有什么值得单曲循环的伤感歌呢
洛克王国要想成为骑士的任务“海边猫头鹰注视
阜南赵集凶杀案
五十岁的女人做cT后当天能跳广场舞舞吗
推荐资讯
试编写算法求单循环链表的表长
运动速率为2*10^5m/s的电子波长
人们说话用词不当仅是因为对词语理解不透彻吗
一个人对你说暂时不在一起,会给你惊喜的是什
怎么考温州六中
求好看的花,最好有什么传说故事的,漂亮点,
市政道路工程,道路土方计算,原始高程,设计
什么是麦尔逊悬挂
小动物找工作作文200
qq里如果开了svip可以用vip的主题或功能吗?
大华印刷厂地址在哪,我要去那里办事
东方朔里 卫青和平阳公主结婚在哪集
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?