⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myserverthread.java

📁 一个初学者的例子
💻 JAVA
字号:
/*
eGraffiti by XdebugX
xdebugx@hotmail.com
http://xdebugx.deltaarc.net
*/

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

public class MyServerThread extends Thread {
    private Socket socket;


    public MyServerThread(Socket socket) {
		this.socket = socket;


	}

    public void run() {
		try {
			DataInputStream in = new DataInputStream(socket.getInputStream());

		    String inputLine;
			while (true) {

	    		inputLine = in.readUTF();
	    		System.out.println (inputLine);


	    			int g,p,c,numChars,wCount;
					char imagesRead[] = new char [1000000];
					String addLine="";


					c=0;
					p=0;
					numChars=0;
					wCount=0;

					waitFileLock ();
					lockFile (false);

					try {
					FileReader fr = new FileReader ("guestbook.dat");
					c=fr.read();
					while (c != -1) {
					imagesRead[numChars]=(char) (c);
					numChars++;
					if (numChars>=1000000) c=-1;
					c=fr.read();
					}
					fr.close();
					} catch (IOException ioe) {System.out.println ("io error for read images webpage");}


					for (p=0;p<inputLine.length();p++) {
					imagesRead[numChars++]=inputLine.charAt(p);
					}


					try {
						FileWriter fw = new FileWriter ("guestbook.dat");

						for (p=0;p<numChars;p++) {
							fw.write (imagesRead[p]);
						}

						fw.close ();
						System.out.println ("closed");

						} catch (IOException ioe) {System.out.println ("io error for write images webpage");}


						lockFile (true);


			}
		} catch (IOException ie) {
	    	ie.printStackTrace();
	    }

}

private void waitFileLock () {
					int c=0;
					String inputLine="";
					FileReader fr;
					boolean locked=true;

					while (locked==true) {
					inputLine="";
					try {
					fr = new FileReader ("lock.txt");
					c=fr.read();
					while (c != -1) {
					inputLine = inputLine + (char) (c);
					c=fr.read();
					}
					fr.close();
					} catch (IOException ioe) {System.out.println ("io error for read lock file");}
					if (inputLine.equals ("ready")) locked=false;
					if (locked==true) {
					try {
					this.sleep (100);
					} catch (InterruptedException e) { }
					System.out.println ("Waiting on file lock");
					}
					}// locked =true


}/// waitFileLock


private void lockFile (boolean openClose) {
					int p=0;
					String inputLine;
					FileWriter  fw;

					if (openClose==false) inputLine="locked";
					else
					inputLine="ready";
					try {
						fw = new FileWriter ("lock.txt");

						for (p=0;p<inputLine.length();p++) {
							fw.write (inputLine.charAt(p));
						}

						fw.close ();
						} catch (IOException ioe) {System.out.println ("io error for write lock.txt");}

}/// FileLock

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -