VC客户端与Java服务端Socket通讯简例
最近接了一个项目,需要做一个客户端从射频卡读卡器读出卡号,然后发给服务器,服务器查询后返回true或false。查了很多网站都没有人写出什么成功的VC客户端与Java服务端通讯的成功代码,都是错误代码,也没有写出正确结果就结贴了。
还好经过半天努力,终于查出了错误,成功修改,下面来看看代码:
VC客户端代码:
VC客户端
1{
2 WSADATA WSAData;
3 int errorcode = 0;
4 static SOCKET remote_server;
5 static struct sockaddr_in server;
6 WORD version = MAKEWORD(2,0);
7 if(0!=WSAStartup(version,&WSAData))
8 {
9 MessageBox("WSAStratUp error__");
10 }
11 remote_server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
12 server.sin_family = AF_INET;
13
14 server.sin_port = htons(9999); //定义端口号
15
16 server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //定义服务器IP地址,此为本地IP
17
18 char buff[100] = "Something/r/n"; //我的VC学的不好,send里面要求传入char *,我也尝试过char * = "Something/r/n"不过传到服务器就只剩前4个字节数据了,原因不明;
19 char buff2[6] = "END\r\n";
20 //发送截止信息,告诉服务器此次通话结束。有些人关闭客户端,服务器段立刻出现IO Exception,原因很简单,举个例子说就像两个人谈话,不可能突然终止,肯定是一个说Bye后,终止,之后离开
21 int k = connect(remote_server,(struct sockaddr*)&server,sizeof(struct sockaddr));
22 if(k!=0)
23 AfxMessageBox("连接错误");
24 else
25 {
26 send(remote_server,buff,sizeof(buff),0);
27 }
28 char buff3[8] = ""; //传进去一个char *,以接收服务器返回值
29 recv(remote_server,buff3,sizeof(buff3),0);
30 m_Edit1 = buff[100];
31 m_Edit2 = buff3; //把数据传进两个Edit框,搞定
32 UpdateData(false);
33 send(remote_server,buff2,sizeof(buff2),0);
34}
1{
2 WSADATA WSAData;
3 int errorcode = 0;
4 static SOCKET remote_server;
5 static struct sockaddr_in server;
6 WORD version = MAKEWORD(2,0);
7 if(0!=WSAStartup(version,&WSAData))
8 {
9 MessageBox("WSAStratUp error__");
10 }
11 remote_server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
12 server.sin_family = AF_INET;
13
14 server.sin_port = htons(9999); //定义端口号
15
16 server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //定义服务器IP地址,此为本地IP
17
18 char buff[100] = "Something/r/n"; //我的VC学的不好,send里面要求传入char *,我也尝试过char * = "Something/r/n"不过传到服务器就只剩前4个字节数据了,原因不明;
19 char buff2[6] = "END\r\n";
20 //发送截止信息,告诉服务器此次通话结束。有些人关闭客户端,服务器段立刻出现IO Exception,原因很简单,举个例子说就像两个人谈话,不可能突然终止,肯定是一个说Bye后,终止,之后离开
21 int k = connect(remote_server,(struct sockaddr*)&server,sizeof(struct sockaddr));
22 if(k!=0)
23 AfxMessageBox("连接错误");
24 else
25 {
26 send(remote_server,buff,sizeof(buff),0);
27 }
28 char buff3[8] = ""; //传进去一个char *,以接收服务器返回值
29 recv(remote_server,buff3,sizeof(buff3),0);
30 m_Edit1 = buff[100];
31 m_Edit2 = buff3; //把数据传进两个Edit框,搞定
32 UpdateData(false);
33 send(remote_server,buff2,sizeof(buff2),0);
34}
Java服务端代码:
MultiJabberServer类代码
1import java.io.IOException;
2import java.net.ServerSocket;
3import java.net.Socket;
4
5public class MultiJabberServer {
6 static final int PORT = 9999;
7 public static void main(String[] args)
8 throws IOException {
9 ServerSocket s = new ServerSocket(PORT);
10 System.out.println("Server Started");
11 try {
12 while(true) {
13 // Blocks until a connection occurs:
14 Socket socket = s.accept();
15 try {
16 new CardServer(socket);
17 } catch(IOException e) {
18 // If it fails, close the socket,
19 // otherwise the thread will close it:
20 socket.close();
21 }
22 }
23 } finally {
24 s.close();
25 }
26 }
27 }
1import java.io.IOException;
2import java.net.ServerSocket;
3import java.net.Socket;
4
5public class MultiJabberServer {
6 static final int PORT = 9999;
7 public static void main(String[] args)
8 throws IOException {
9 ServerSocket s = new ServerSocket(PORT);
10 System.out.println("Server Started");
11 try {
12 while(true) {
13 // Blocks until a connection occurs:
14 Socket socket = s.accept();
15 try {
16 new CardServer(socket);
17 } catch(IOException e) {
18 // If it fails, close the socket,
19 // otherwise the thread will close it:
20 socket.close();
21 }
22 }
23 } finally {
24 s.close();
25 }
26 }
27 }
CardServer 类代码
1import java.net.Socket;
2import java.io.*;
3
4public class CardServer extends Thread {
5 private Socket socket;
6 private BufferedReader in;
7 private PrintWriter out;
8 public static int i = 1;
9
10 public CardServer(Socket s)
11 throws IOException {
12 socket = s;
13 in =
14 new BufferedReader(
15 new InputStreamReader(
16 socket.getInputStream()));
17 // Enable auto-flush:
18 out =
19 new PrintWriter(
20 new BufferedWriter(
21 new OutputStreamWriter(
22 socket.getOutputStream())), true);
23 // If any of the above calls throw an
24 // exception, the caller is responsible for
25 // closing the socket. Otherwise the thread
26 // will close it.
27 start(); // Calls run()
28 }
29
30 public void run() {
31 try {
32 while (true) {
33 String str = in.readLine();
34 //这里补充下刚才VC客户端传的值的问题,因为Java这里写的是readLine(),所以给服务器传值时别忘了在最后加上\n或者\r\n,要不你会为服务器收不到数据而抓狂的
35 if (str.trim().equals("END")) break;
36 System.out.println("Echoing:" + str.trim());
37
38 //查询数据库,返回值
39 out.println("ture");
40
41 }
42 System.out.println("closing");
43 } catch (IOException e) {
44 System.err.println("IO Exception");
45 } finally {
46 try {
47 socket.close();
48 } catch (IOException e) {
49 System.err.println("Socket not closed");
50 }
51 }
52 }
53}
1import java.net.Socket;
2import java.io.*;
3
4public class CardServer extends Thread {
5 private Socket socket;
6 private BufferedReader in;
7 private PrintWriter out;
8 public static int i = 1;
9
10 public CardServer(Socket s)
11 throws IOException {
12 socket = s;
13 in =
14 new BufferedReader(
15 new InputStreamReader(
16 socket.getInputStream()));
17 // Enable auto-flush:
18 out =
19 new PrintWriter(
20 new BufferedWriter(
21 new OutputStreamWriter(
22 socket.getOutputStream())), true);
23 // If any of the above calls throw an
24 // exception, the caller is responsible for
25 // closing the socket. Otherwise the thread
26 // will close it.
27 start(); // Calls run()
28 }
29
30 public void run() {
31 try {
32 while (true) {
33 String str = in.readLine();
34 //这里补充下刚才VC客户端传的值的问题,因为Java这里写的是readLine(),所以给服务器传值时别忘了在最后加上\n或者\r\n,要不你会为服务器收不到数据而抓狂的
35 if (str.trim().equals("END")) break;
36 System.out.println("Echoing:" + str.trim());
37
38 //查询数据库,返回值
39 out.println("ture");
40
41 }
42 System.out.println("closing");
43 } catch (IOException e) {
44 System.err.println("IO Exception");
45 } finally {
46 try {
47 socket.close();
48 } catch (IOException e) {
49 System.err.println("Socket not closed");
50 }
51 }
52 }
53}
好啦,简单的例子结束了,希望他能够帮你的忙,希望你不要像我似的要忙一天才写这么点简单的东西。代码都是七拼八凑的,我也忘了都复制谁的了,总之感谢这些人吧。
//我的VC和C实在是不好,所以当你看到VC那边的代码数据类型转换有点繁琐或者多余,请见谅啊