int port = 8080;
string host = "192.168.1.123";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndPoint实例
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一 Console.WriteLine("Conneting.。。");
c.Connect(ipe);//连接到服务器
                
  //采用异步方式发送 并且UDP貌似不行 丢失太
byte[] bs = Encoding.ASCII.GetBytes(TextEncoder.ByteArrayToHexString(bTmp));
Console.WriteLine("Send Message: {0}", TextEncoder.ByteArrayToHexString(bTmp));
c.Send(bs, bs.Length, 0);//发送测试信息,貌似不能成功啊!!!!!!!!!!
                  
怎么连续发呢、、
而且现在是一次不能全部发完信息,信息需要一次次的从读卡器上读。。读卡器这个不用考虑了
假如我的端口为8080,IP为192.168.1.1,现在一个程序一直等待我发数据,我该怎么做呢?我利用Socket函数 弄了N天了 还是搞不定啊  不能连续的发啊  求大神给我指导指导 最好给出关键代码和注释?谢谢了。、、、神人们

解决方案 »

  1.   

    接收方需要监听这个SOCKET是否发送了数据,   重点在接收那边
      

  2.   

    package backend;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;/**
     *
     * @author bluecat
     */
    public class RFlistenerThread extends Thread{    private ServerSocket server = null;
        private Socket client = null;
        private BufferedReader in = null;
        private IRFcallback callback;
        private String id = "0";
        public RFlistenerThread(IRFcallback callback) {
            this.callback = callback;
            this.setName("RFlistener-thread-"+id);
            this.listen(8080);
        }
        public RFlistenerThread(String id, IRFcallback callback, int port) {
            this.id = id;
            this.callback = callback;
            this.setName("RFlistener-thread-"+id);
            this.listen(port);
        }    private void listen(int port) {
            try {
                server = new ServerSocket(port);
            } catch (IOException e) {
                System.out.println(id + ": Could not listen on port " + port);
                System.exit(-1);
            }        try {
                System.out.println(id + ": wait for rf device to connect");
                client = server.accept();
            } catch (IOException e) {
                System.out.println(id + ": Accept failed: " + port);
                System.exit(-1);
            }
            System.out.println(id + ": device connected");
            try {
                in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    //            out = new PrintWriter(client.getOutputStream(), true);
            } catch (IOException e) {
                System.out.println(id + ": Accept failed");
                System.exit(-1);
            }
        }    @Override
        public void run() {
            while (!callback.isFinished()) {
                try {
                    System.out.println(id + ": wait for data from device");
                    
    //                String rfid = in.readLine().trim();
                    String temp, temp1, temp2;
                    temp = in.readLine();
                    temp1 = temp.trim();
                    temp2 = temp1.toUpperCase();
                    String rfid = temp2;
                    System.out.println(temp+" "+temp1+" "+temp2);
    //                String rfid = in.readLine().trim().toUpperCase();
                    System.out.println(id + ": received data " + rfid);
                    callback.onReceive(id, rfid);
                } catch (IOException e) {
                    System.out.println(id + ": Read failed");
    //                System.exit(-1);
                } catch (NullPointerException e) {
                    System.out.println(id+":sdfaf");
                }
            }
            System.out.println(id + ": exits");
        }    @Override
        protected void finalize(){
            try {
                in.close();
                server.close();
            } catch (IOException e) {
                System.out.println(id + ": Could not close.");
                System.exit(-1);
            }
        }    public static void main(String[] args) {
        // a simple test
            RFlistenerThread t = new RFlistenerThread(
                    new IRFcallback() {
                        private int i = 10;
                        public boolean isFinished() {
                            return i == 0;
                        }
                        public void onReceive(String deviceID, String rfid) {
                            System.out.println("sample callback: received rfid:" + rfid);
                            i--;
                        }
                    });
            t.start();
        }    }