socket 怎么做到客户发一条服务器接受一条,再发给另一个客户端,如此循环往复

解决方案 »

  1.   


    package com.lh.socket;import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.net.InetSocketAddress;
    import java.net.Socket;/**
     * 客户端发送类
     * @author -Rayn
     */
    public class ClientTcpSend
    {
    public static String clientip = "127.0.0.1";
    public static int port = 33456;

    public static void main(String[] args)
    {
    int length = 0;
    byte [] sendBytes = null;
    Socket socket = null;
    DataOutputStream dos = null;
    FileInputStream fis = null;

    try
    {
    try
    {
    socket = new Socket();
    socket.connect(new InetSocketAddress(clientip, port), 30 * 1000);
    dos = new DataOutputStream(socket.getOutputStream());
    File file = new File("E:/aa.jpg");
    fis = new FileInputStream(file);
    sendBytes = new byte[1024 * 4];
    while((length = fis.read(sendBytes, 0, sendBytes.length)) > 0)
    {
    dos.write(sendBytes, 0, length);
    dos.flush();
    }
    }
    catch (Exception e)
    {
    // TODO: handle exception
    e.printStackTrace();
    }
    finally
    {
    if(dos != null)
    dos.close();
    if(fis != null)
    fis.close();
    if(socket != null)
    socket.close();
    }
    }
    catch (Exception e)
    {
    // TODO: handle exception
    e.printStackTrace();
    }
    }
    }
      

  2.   

    package com.lh.socket;import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;public class ServerTcpListener implements Runnable
    {
    public static void main(String[] args)
    {
    try
    {
    final ServerSocket server = new ServerSocket(ClientTcpSend.port);
    Thread th = new Thread(new Runnable()
    {
    @Override
    public void run()
    {
    while(true)
    {
    System.out.println("开始监听.....");
    Socket socket = null;
    try
    {
    socket = server.accept();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println("有链接...");
    receiveFile(socket);
    }
    }
    });
    th.run();
    }
    catch (Exception e)
    {
    // TODO: handle exception
    e.printStackTrace();
    }
    }


    @Override
    public void run()
    {
    // TODO Auto-generated method stub
    }

    public static void receiveFile(Socket socket)
    {
    byte [] inputByte = null;
    int length = 0;
    DataInputStream dis = null;
    FileOutputStream fos = null;

    try
    {
    try
    {
    dis = new DataInputStream(socket.getInputStream());
    fos = new FileOutputStream(new File("D:/aa.jpg"));
    inputByte = new byte[1024 * 4];
    System.out.println("开始接受数据");
    while((length = dis.read(inputByte, 0, inputByte.length)) > 0)
    {
    fos.write(inputByte, 0, length);
    fos.flush();
    }
    System.out.println("完成接受...");
    }
    catch (Exception e)
    {
    // TODO: handle exception
    e.printStackTrace();
    }
    finally
    {
    if(fos != null)
    fos.close();
    if(dis != null)
    dis.close();
    if(socket != null)
    socket.close();
    }
    }
    catch (Exception e)
    {
    // TODO: handle exception
    e.printStackTrace();
    }
    }
    }
      

  3.   

    没明白说下connect的用法,要怎么建立连接,然后怎么重复发送,重复接收
      

  4.   

    还有,客户端writeUTF后是不是客户端的readUTF马上更新了
      

  5.   

    对的,就弄个死循环,首先Client发送,然后Server接收,然后Server发送,Client接收。
    两边都起DataInputStream和DataOutputStream吧。不过一般是客户端只接收Client的连接,然后保存每一个Client的连接,然后Client发送一句,首先发到Server,Server再转发给其他客户端
      

  6.   

    import java.io.*;
    import java.net.*;
    import java.sql.*;
    class ser1 implements Runnable{
    ServerSocket ss;
        Socket sc;
        DataInputStream in=null;
        DataOutputStream out=null;
        InputStream in_data;
        OutputStream out_data;
        String str;
        public ser1() {
         try{
         ss=new ServerSocket(1234);
         while(true){
    sc=ss.accept();
    Thread m=new Thread(this);
    m.start();
    }
         }
         catch(IOException ee){
         System.out.println("错误");
         }
        }
    public void run() {
    try{
    while(true){
    in_data=sc.getInputStream();
    out_data=sc.getOutputStream();
    in=new DataInputStream(in_data);
    out=new DataOutputStream(out_data);

    str=in.readUTF();
    out.writeUTF(str);
    if(str.equals("end")){
    in.close();
    out.close();
    sc.close();
    } }
    }
    catch(IOException e){
    System.out.println("输入输出流错误");
    } }

    }
    class ser2 implements Runnable{
    ServerSocket ss;
        Socket sc;
        DataInputStream in=null;
        DataOutputStream out=null;
        InputStream in_data;
        OutputStream out_data;
        String str;
        String driver="sun.jdbc.odbc.JdbcOdbcDriver";             
    static String db="D:\\Java\\JavaWork\\NWIND.MDB"; 
    String url1="jdbc:odbc:NWIND";
    Connection conn=null;
    Statement stmt=null;
        PreparedStatement ps=null;
    ResultSet rs=null;
    String user="";
        String pwd="";
        String a="select * from 学生  where 姓名=";
        String s1;
        String s2;
        
        public void connect()throws SQLException { 
      try {
      Class.forName(driver);
          conn = DriverManager.getConnection(url1,user,pwd);
          stmt = conn.createStatement();
          rs=stmt.executeQuery(a+str);
          while (rs.next()){
           s1=rs.getString(1);
           s2=rs.getString(2);
           }
      }catch (Exception e){
       System.out.println(e);
      }finally{
       stmt.close();
       conn.close();
      } 
     }
         public ser2() {
         try{
         ss=new ServerSocket(1234);
         while(true){
    sc=ss.accept();
    Thread m=new Thread(this);
    m.start();
    }
         }
         catch(IOException ee){
         System.out.println("错误");
         }
        }
    public void run() {
    try{
    while(true){
    in_data=sc.getInputStream();
    out_data=sc.getOutputStream();
    in=new DataInputStream(in_data);
    out=new DataOutputStream(out_data);
    str=in.readUTF();
    String[] ary = str.split(" ");
    while(str.equals("end"))



    if(ary[0].equals(s1)){
    if(ary[2].equals(s2)){
    new ser1();
    out.writeUTF("密码正确");
    break;

    }
    else if(ary[2]!=s2){
    out.writeUTF("密码错误");
        
    }
    }

    in.close();
    out.close();
    sc.close();


    }
    }
    catch(IOException e){
    System.out.println("输入输出流错误");
    } }
     
    }
    class ser{
    public static void main(String[] args){ new ser2();
    }
    }
    结果是:
    错误