/*服务器*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define szSTR 256
#define SERVERPORT 21429 /*please modify the port with your id*/
int do_listen(const int port, const int bTcp)
{
int s = 0, r = 0, o = 1;
struct sockaddr_in h;
memset(&h, 0, sizeof(h));
h.sin_family = AF_INET; h.sin_port = htons(port);
h.sin_addr.s_addr = INADDR_ANY;
s = socket(AF_INET, bTcp?SOCK_STREAM:SOCK_DGRAM, 0);
if (s < 1) { perror("socket(listen)"); return 0;}
r = setsockopt(s, SOL_SOCKET,SO_REUSEADDR, (char *)&o, sizeof(int));
if (r == -1) { perror("setsockopt(listen)"); return 0;}
r = bind(s, (struct sockaddr *)&h, sizeof(h));
if (r == -1) { perror("bind(listen)"); return 0;}
if (bTcp) {
r = listen(s, SOMAXCONN);
if (r == -1) { perror("listen()"); return 0;}
}/*end if*/
/*signal(SIGPIPE, SIG_IGN);*/
return s;
}/*end do_listen*/
void response(int sck, struct sockaddr_in * host)
{
FILE * f = 0; char cmd[szSTR]; time_t now;
struct tm * t = 0;
f = fdopen(sck, "w+");
while(!feof(f)) {
memset(cmd, 0, szSTR);
fgets(cmd, szSTR -1, f);
time(&now);
t = localtime(&now);
if(strstr(cmd, "DATE")) {
fprintf(stderr, "Query Type: Date\n");
fprintf(f, "Date: %d %d, %d\n", t->tm_mon + 1, t->tm_mday, t->tm_year + 1900);
continue;
}/*end if*/
if(strstr(cmd, "TIME")) {
fprintf(stderr, "Query Type: Time\n");
fprintf(f, "Time: %02d::%02d::%02d\n", t->tm_hour, t->tm_min, t->tm_sec);
continue;
}/*end if*/
if(strstr(cmd, "EXIT")) break;
fprintf(f, "commands: DATE, TIME, EXIT.\n");
}/*end while*/
shutdown(sck, SHUT_RDWR);
close(sck);
fclose(f);
return ;
}/*end response*/
int main(void)
{
socklen_t sklen = 0;int sck = 0, i = 0, listener = 0;
struct sockaddr_in client; pid_t proc = 0;
system("ifconfig");
listener = do_listen(SERVERPORT, 1);
if(listener < 1) { perror("listen()"); return 0; }
for(i=0;i<2;i++) {
memset(&client, 0, sizeof(client));
sklen = sizeof(client);
sck = accept(listener, (struct sockaddr *)&client, &sklen);
if(sck < 1) break;
proc = fork();
if (proc == -1) { perror("fork()"); break ; }
if(proc) continue;
response(sck, &client);
break;
}/*next*/
close(listener);
return 0;
}/*end main*/
/*客户机*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define szSTR 256
#define SERVERPORT 21429 /*please modify the port with your id*/
int cnn(const char * ip, const int port)
{
struct sockaddr_in h; memset(&h, 0, sizeof(h));
h.sin_family = AF_INET; h.sin_port = htons(port);
h.sin_addr.s_addr = inet_addr(ip);
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 1) { perror("socket(tcp)"); return 0;}
int r = connect(s, (struct sockaddr *)&h, sizeof(h));
if (r == 0) return s;
perror("connect()");
return 0;
}//end cnn
int main(void)
{
int sck = 0; FILE * f = 0; char ip[szSTR]="127.0.0.1";
fprintf(stderr, "Please input the calendar server ip:");
fgets(ip, szSTR - 1, stdin);
sck = cnn(ip, SERVERPORT);
if(sck < 1) return 0;
f = fdopen(sck, "w+");
fprintf(f, "DATE\r\n");
fgets(ip, szSTR -1 , f);
fprintf(stderr, "%s\n", ip);
fprintf(f, "TIME\r\n");
fgets(ip, szSTR -1 , f);
fprintf(stderr, "%s\n", ip);
fprintf(f, "EXIT\r\n");
fclose(f);
close(sck);
return 0;
}/*end main*/
我这有一段最简单的SOCKET连接 服务端的~! 前阵子装Ubuntu 10 后写着实验用的,这是最基本的TCP 服务端框架。各种函数的条用跟 WINDOWS下的一样(我说的WINDOWS下可不是MFC喔~!)客户端自己琢磨,不懂的话再发你~!至于你要的细节,我感觉还是自己写的好
人不能太懒~!必须深入认识否则你领略不到 C程序设计 的精髓~!
//////////代码如下//////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
///////////////////////// H*F*W 2010.05.21 In GuangZhou ///////////////////////////
///////////////////////// LINUX SOCKET 实验最简单服务端 ///////////////////////////
/////////////////////// linux 编程 SOCKET,GTK 有共同兴趣的朋友 +我 Q: 176469428 ////////
#include
#include
#include
#include
int Hlisten(unsigned short hport)
{
int hsock,rhsock,cc,alen,ret;
struct sockaddr_in hlsock;
char hbuf[65535];
if((hsock=socket(AF_INET,SOCK_STREAM,0))==-1){printf("Error in socket()\n");return 0;} //建立套接字
memset(&hlsock,0,sizeof(hlsock)); //初始化SOCKET空间
hlsock.sin_family=AF_INET;
hlsock.sin_port=htons(hport); ///监听端口
hlsock.sin_addr.s_addr=htonl(INADDR_ANY); ///地址族
ret=1;
setsockopt(hsock,SOL_SOCKET,SO_REUSEADDR,&ret,sizeof(ret));
if(bind(hsock,(struct sockaddr*)&hlsock,sizeof(hlsock))==-1){printf("Error in bind()\n");return 0;} //帮定
if(listen(hsock,0)==-1){printf("Error in listen()\n");return 0;} ///进入监听状态
alen=sizeof(hlsock);
rhsock=accept(hsock,(struct sockaddr*)&hlsock,&alen); ///接受 客户端接入
while(1)
{
memset(hbuf,0,sizeof(hbuf));
cc=recv(rhsock,hbuf,sizeof(hbuf),0); ///等待接收数据
if(cc==-1)break;
printf("%s",hbuf); ///显示接收内容
}
close(hsock); //关闭连接
return 0;
}
int main(int argc,char*argv[])
{
//if(argc<=1){printf("usge: [port]");return 0;}
Hlisten(330); //调用监听过程 传入监听端口
return 0;
}
///////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
////////////////// 使用gcc 编译直接就可以看到效果////////////////
/////////////////////////////////////////////////////////////////
《TCP/IP Socket网络编程基础(C语言版)》里面有例程,可以去书里说的网站去下载一份例程,我记得有客户端和服务器端的程序,不过好像是一对一的。可以参考一下,多学习一点总没有坏处。
帮顶~等高手