全部博文(2065)
分类: C/C++
2010-03-13 12:00:08
C与JAVA之间的TCP通讯连接
[整理人:遥方 整理时间:
基于目前的一个架构形式。服务层使用C来编写。客户端调用使用JAVA。模拟一个SOCKET通讯过程方便后面的代码编写。
服务层代码如下(xsocket.c)
/**
作用:一个socket服务器
*/
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 2004
int init_socket(void);
int bind_port(int socket_fd);
int accept_client(int
socket_fd);
int rec_mess(int socket_fd,char
*mess);
int tofile(char *mess,FILE
*fp);
int main(void)
{
int ret_init_socket = 0;
int ret_bind_port = 0;
int ret_listen = 0;
int ret_accept_client = 0;
int ret_tofile = 0;
int ret_recmessage = 0;
int flag = 1;
char *temp = "END";
char message[2049];
struct sockaddr_in test_sockaddr;
FILE
*fp;
ret_init_socket = init_socket();
if(ret_init_socket < 0)
{
perror("creat socker error");
exit(-1);
}
ret_bind_port = bind_port(ret_init_socket);
if (ret_bind_port < 0)
{
perror("bind port error");
}
ret_listen = listen(ret_init_socket,5);
if(ret_listen < 0)
{
perror("Listen port error");
exit(-1);
}
ret_accept_client =
accept_client(ret_init_socket);
if(ret_accept_client < 0)
{
perror("accept error \n");
exit(-1);
}
memset(message,0,sizeof(message));
fp =
fopen("message.txt","a+");
while(1)
{
ret_recmessage =
rec_mess(ret_accept_client,message);
if (ret_recmessage <
0)
{
perror("rec_message
error");
exit(-1);
}
if (ret_recmessage < 5
&& (!strncmp(temp,message,3)))
{
printf("End of rec
file\n");
exit(0);
}
ret_tofile =
tofile(message,fp);
if (ret_tofile < 0)
{
perror("write file
error");
exit(-1);
}
memset(message,0,sizeof(message));
send_client(ret_accept_client);
fflush(fp);
}
fclose(fp);
shutdown(ret_accept_client,2);
shutdown(ret_init_socket,2);
}
/**
Comment :
init socket server
*/
int init_socket(void)
{
int ret;
ret = socket(AF_INET,SOCK_STREAM,0);
if (ret == -1)
{
perror("sock creating error");
exit(-1);
}
return ret;
}
int bind_port(int socket_fd)
{
int ret;
struct sockaddr_in serv_addr;
bzero(&serv_addr,sizeof(struct
sockaddr_in));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVER_PORT);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(socket_fd,(struct
sockaddr*)&serv_addr,sizeof(struct sockaddr));
if (ret < 0)
{
perror("bind to SERVER_PORT
error");
exit(-1);
}
return ret;
}
int accept_client(int
socket_fd)
{
size_t ret;
int len = 0;
struct sockaddr_in client_addr;
len = sizeof(struct sockaddr);
ret = accept(socket_fd,(struct sockaddr
*)&client_addr,&len);
if(ret == -1)
{
perror("accept client error");
exit(-1);
}
return ret;
}
/**
comment:
accept msg
*/
int rec_mess(int socket_fd,char *mess)
{
int ret=(size_t)0;
ret = recv(socket_fd,mess,2048,0);
if(ret < 0)
{
perror("recv error");
exit(-1);
}
return ret;
}
int tofile(char *mess,FILE *fp)
{
int ret,ret_fseek;
fpos_t pos;
if (fp == NULL)
{
perror("don’t creat file to save
message");
exit(-1);
}
fseek(fp,0,SEEK_END);
fgetpos(fp,&pos);
ret = fputs(mess, fp);
if (ret < 0)
{
perror("write file error");
exit(-1);
}
return ret;
}
int send_client(int socket_fd)
{
int ret;
char *temp ="OK";
ret = send(socket_fd,temp,3,0);
if (ret < 0)
{
perror("send error");
exit(-1);
}
return 0;
}
编写完成之后gcc之后直接运行一下就OK了!
此时检查端口是否启来:netstat –a|grep
2004
二、编写java的客户端代码如下
import java.io.*;
import java.net.Socket;
public class TestDB {
Socket socket;
BufferedReader in;
PrintWriter out;
public TestDB(){
try {
socket = new Socket("124.XXX.XXX.XXX
", 2004);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
out.println(line.readLine());
line.close();
out.close();
in.close();
socket.close();
}catch (IOException e){}
}
public static void main(String[] args) {
new TestDB();
}
}
编译之后执行。成功!
# java Client
connect to server
结论:只需要创建出来一个高并发的socket服务器(建议用C实现),然后客户端可以提供多API调用的方式这样可以构建出来一个大型的B/S架构。效率我想比XSocket的应该要高。毕竟JAVA跑起来的速度是比C相差N个数量级的事。下一步准备构建这个应用出来。功能代码由c实现然后应用那边直接启一个client就OK了!