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天了 还是搞不定啊 不能连续的发啊 求大神给我指导指导 最好给出关键代码和注释?谢谢了。、、、神人们
解决方案 »
- 为何不能添加服务引用?
- 自定的类如何才能用作dictionary的key?
- 唉,再发一问,还是SQL语句
- 软件升级
- 用子线程调用一个窗体,窗体上加载了一个webBrowser,调用webBrowser.Navigate(url);没有任何反应!!
- WinForm窗体打印问题
- 奇怪的问题:文本日志和数据库都写不进数据,但程序也不报错
- 探討一下.net里面的服務器端和客戶端互相調用時注意和存在的問題,有興趣可以進來參加討論,散分
- 参与者有分...........
- 数据量在十万级时多线程导出Excel,怎么做呢?
- 提示str.Trim() 未将对象引用设置到实例,应该怎样将str中的数据引用
- 如何将字符串0.00转化为数字0
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();
} }