socket data

      ::键盘时代:: 2005-10-12 23:9
get an java socket example,I want to try xmlSocket in flash.

post here for study.

Server:

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

public class SingleServer
{
    public static void main(String args[]){
        try{
            ServerSocket serverObj=null;
            try{
                serverObj=new ServerSocket(3000);
            }catch(Exception e){
                System.out.println("Sorry,can not listen to"+e);
            }
            Socket clientObj=null;
            try{
                clientObj=serverObj.accept();
            }catch(Exception e){
                System.out.println("Error server:"+e);
            }
            String lineStr;
            InputStreamReader streamObj1=new InputStreamReader(clientObj.getInputStream());
            BufferedReader buffObj1=new BufferedReader(streamObj1);
            PrintWriter outObj=new PrintWriter(clientObj.getOutputStream());
            InputStreamReader streamObj2=new InputStreamReader(System.in);
            BufferedReader buffObj2=new BufferedReader(streamObj2);
            System.out.println("Client:"+buffObj1.readLine());
            lineStr=buffObj2.readLine();
            while(!lineStr.equals("finish")){
                outObj.println(lineStr);
                outObj.flush();
                System.out.println("Server:"+lineStr);
                System.out.println("Client:"+buffObj1.readLine());
                lineStr=buffObj2.readLine();
            }
            buffObj1.close();
            outObj.close();
            clientObj.close();
            serverObj.close();
        }catch(Exception e){
            System.out.println("Error:"+e);
        }
    }
};

------------------------------
client:
import java.io.*;
import java.net.*;

public class SingleClient
{
    public static void main(String args[]){
        try{
            Socket socketObj = new Socket("127.0.0.1",3000);
            InputStreamReader streamObj1=new InputStreamReader(System.in);
            BufferedReader buffObj1=new BufferedReader(streamObj1);
            PrintWriter outObj = new PrintWriter(socketObj.getOutputStream());
            BufferedReader buffObj2=new BufferedReader(new InputStreamReader(socketObj.getInputStream()));
            String readStr;
            readStr=buffObj1.readLine();
            while(!readStr.equals("finish")){
                outObj.println(readStr);
                outObj.flush();
                System.out.println("Client:"+readStr);
                System.out.println("Server:"+buffObj2.readLine());
                readStr=buffObj1.readLine();
            }
            outObj.close();
            buffObj2.close();
            socketObj.close();
        }catch(Exception e){
            System.out.println("Error:"+e);
        }
    }
};

code in flash:
var mySocket = new XMLSocket();
mySocket.connect("127.0.0.1", 3000);
mySocket.onConnect = function(success) {
    if (success) {
        my.text="Ok";
    } else {
        my.text="connect failed";
    }
};

I will make an exhaustive study in a week.
I hope to get a good work
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}