Java客户端 C++服务器端

Java 客户端

import java.io.BufferedReader;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.InputStreamReader;   
import java.io.OutputStream;   
import java.io.PrintWriter;   
import java.net.InetSocketAddress;   
import java.net.Socket;   
  
/**  
 * 客户端  
 *   
 * @author Johnson Lee  
 *   
 */  
public class Client {   
    private InetSocketAddress svrAddress;   
    private Socket svrSocket;   
  
    public Client(String host, int port) {   
        this.svrAddress = new InetSocketAddress(host, port);   
    }   
  
    public void connect() throws IOException {   
        this.svrSocket = new Socket(svrAddress.getAddress(), svrAddress   
                .getPort());   
    }   
  
    public boolean isClosed() {   
        return this.svrSocket == null || this.svrSocket.isClosed();   
    }   
  
    public InputStream getInputStream() throws IOException {   
        return this.svrSocket.getInputStream();   
    }   
  
    public OutputStream getOutputStream() throws IOException {   
        return this.svrSocket.getOutputStream();   
    }   
  
    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
        BufferedReader br = null;   
        PrintWriter pw = null;   
        String line = null;   
        try {   
            final Client c = new Client("192.168.1.11", 9090);   
            c.connect();// 连接服务器   
            try {   
                br = new BufferedReader(new InputStreamReader(System.in));   
                pw = new PrintWriter(c.getOutputStream(), true);   
                new Thread(new ResponseProcessor(c)).start();   
                while (!c.isClosed()) {   
                    line = br.readLine();   
                    pw.println(line);   
                }   
            } catch (IOException e) {   
                e.printStackTrace();   
            } finally {   
                try {   
                    if (br != null) {   
                        br.close();   
                        br = null;   
                    }   
                    if (pw != null) {   
                        pw.close();   
                        pw = null;   
                    }   
                } catch (IOException e) {   
                }   
            }   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
    }   
  
}  


 

import java.io.BufferedReader;   
import java.io.IOException;   
import java.io.InputStreamReader;   
import java.io.PrintWriter;   
import java.net.SocketException;   
  
/**  
 * 服务端响应处理器  
 *   
 * @author Johnson Lee  
 *   
 */  
public class ResponseProcessor implements Runnable {   
    private Client client;   
  
    public ResponseProcessor(Client c) {   
        super();   
        this.client = c;   
    }   
  
    @Override  
    public void run() {   
        BufferedReader br = null;   
        PrintWriter pw = null;   
        String line = null;   
        try {   
            br = new BufferedReader(new InputStreamReader(client   
                    .getInputStream()));   
            pw = new PrintWriter(System.out, true);   
            while (!client.isClosed()) {   
                line = br.readLine();   
                pw.println(line);   
            }   
        } catch (SocketException e) {   
            if (!client.isClosed()) {   
                System.err.println(e);   
            }   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
    }   
  
}  

C++服务器端

#include "iostream" 
#include <winsock2.h> 
using namespace std; 
#pragma comment(lib,"ws2_32.lib")//载入win32库 
#define MAIN_PORT 2194  //端口号
#define BUFFER_LEN 1024 //数组的长度
void main() {
        WSAData wsaData; 
        WSAStartup(MAKEWORD(2,2),&wsaData);//开启服务 
        SOCKET ListenSock; 
        ListenSock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//初始化监听套接字
        sockaddr_in LocalAddr={0};
        LocalAddr.sin_family=AF_INET;
        LocalAddr.sin_port=htons(MAIN_PORT);
        LocalAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
        ::bind(ListenSock,(sockaddr*)&LocalAddr,sizeof(sockaddr_in));//将监听套接字绑定到本地一端口上 
        listen(ListenSock,5);//设置最大监听值
        cout<<"开始监听!"<<endl; 
        SOCKET ClientSock; //创建与Client连接套接字
        sockaddr_in AptAddr={0};//存储Client的通信地址
        int Addrlen=sizeof(sockaddr_in); 
        cout<<"等待Client端连入!"<<endl; 
        ClientSock=accept(ListenSock,(sockaddr*)&AptAddr,&Addrlen);//等待对方的连入
        cout <<"接收到一条连接!"<<endl; 
        int RcvLen=0; 
        char buf[BUFFER_LEN];
        while(RcvLen>=0) { 
            memset(buf,0,BUFFER_LEN); 
            RcvLen=recv(ClientSock,buf,BUFFER_LEN,0); 
            if(RcvLen>0) 
                cout<<buf<<endl; 
        }
        cout<<"Client 退出!"<<endl; 
        WSACleanup();
    } 

 

posted @ 2012-06-22 09:58  坏混混  阅读(368)  评论(0编辑  收藏  举报