September 27,2005

Java socket server and Client

One of java socket sample code for server and client

Server
package mynet;
import java.io.* ;
import java.net.* ;


public class EchoB {

public static final int ECHOPORT = 7;
public static final int NUM_THREADS = 10;
public static Socket clientSocket;

public static String messa;

public String getmes(){
messa = "ABCDEFG";
return messa;

}

public void setmes(){
messa = "ddddddd";
}

/** 主方法,啟動伺服器 */
public static void main(String[] av)
{
new EchoB(ECHOPORT, NUM_THREADS);
}

/** 建構式 */
public EchoB(int port, int numThreads)
{
ServerSocket servSock;


try {
servSock = new ServerSocket(ECHOPORT);

} catch(IOException e) {
/* 如果 I/O 失敗,伺服器就會崩潰 */
System.err.println("Could not create ServerSocket " + e);
System.exit(1);
return; /* 不會到此 */
}

// 預先產生一組 threads,並啟動它們。
for (int i=0; i new Thread(new Handler(servSock, i)).start();
}
}

/** Handler 是 Thread 的子類別,只負責一位用戶端的對話 */
class Handler extends Thread {
ServerSocket servSock;
int threadNumber;

/** 建構一個 Handler. */
Handler(ServerSocket s, int i) {
super();
servSock = s;
threadNumber = i;
setName("Thread " + threadNumber);
}

public void run()
{
/* 等待連線。
* 在等待 accept() 方法返回時,
* 對 ServerSocket 同步化
*/
while (true){
try {
System.out.println( getName() + " waiting");

Socket clientSocket;
// 在此等待下次連線
synchronized(servSock) {
clientSocket = servSock.accept();
}
System.out.println(getName() + " starting, IP=" +
clientSocket.getInetAddress());
BufferedReader is = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
PrintStream os = new PrintStream(
clientSocket.getOutputStream(), true);


String line;
String aline = getmes();
while ((line = is.readLine()) != null) {
os.print(line + "\r\n");


if(line.equals("Hello")){
os.println("Hi");
os.println("roger");
os.println(aline);
}

os.println("END");
os.flush();
}
System.out.println(getName() + " END ");
clientSocket.close();
} catch (IOException ex) {
System.out.println(getName() + ": IO Error on socket " + ex);
return;
}
}
}
}
}


Client

package mynet;
import java.io.*;
import java.net.*;
public class EchoA extends Thread{

public static String hostName = "localhost";
public static int post = 7;

private Thread updateThread;
int update = 1000;
static int i = 0;
static String mesg = "Hello";

public void run(){

while(true){

try {
Thread.sleep(update);
i++;
EchoClient();
System.out.println("Print Time From Minitor -> "+i);
stops();

}catch(InterruptedException e){

repaint();

}

}
}

public void start(){
if(updateThread ==null){
updateThread = new Thread(this);
updateThread.start();

}
}

public void stops(){
if(updateThread !=null){
updateThread =null;
System.out.println("Listen.......");
}
}

private void repaint() {
System.out.print("Exceptione !!!! In Thread");
}


public void EchoClient(){
try {
Socket sock = new Socket(hostName, post); // echo server.
BufferedReader is = new BufferedReader(new
InputStreamReader(sock.getInputStream()));
PrintWriter os = new PrintWriter(sock.getOutputStream(), true);

while(true){
os.print(mesg + "\r\n");
os.flush();
String reply = is.readLine();
System.out.println("Sent "" + mesg + """);
System.out.println("Got "" + reply + """);
if(reply.contains("END"))break;
}


sock.close();
} catch (IOException e) {
System.err.println(e);
}
}


public static void main(String[] args) {
EchoA a = new EchoA();
a.start();
}
}

Posted by yam_javanull at 樂多Roodo! │02:38 │回應(2)引用(1)Code
樂多分類:網路/3C 共同主題:程式設計 工具:編輯本文
Ads by Roodo! 

引用URL

http://cgi.blog.roodo.com/trackback/527053
引用列表:
all about polomurinureon and top news
polomurinureon 58 post【polomurinureon blog】 at October 24,2007 15:39
回應文章
安你好
我還是一個學生
我們老師要我們找一個echo server的程式
我還是大一對程式設計還嫩的很
看了你po的文
想用看看
可是跑JAVA跑不出來
你可以幫幫我嗎
Posted by 阿健 at October 11,2006 14:04
[URL=http://www.vlassio.cn/egizi] egizi [/URL] egizi [URL=http://www.vlassio.cn/fax-cordless] fax cordless [/URL] fax cordless [URL=http://www.vlassio.cn/tel-aviv-cose-fare] tel aviv cose fare [/URL] tel aviv cose fare
Posted by Aria at June 3,2008 15:08