- 论坛徽章:
- 0
|
要写socket程序,不知多少人写过,有没有现成的源码下载啊
在我的电脑上可以互相发送了.
也就是:前端连上后,后端将发送一个"how do you do "给前端,
前端然后可以发送字符串给后端,后端收到后,便发送一个"your data ,I accept"
回馈到前端.
你用GCC编译运行一下,因为我的电脑没有中文支持,有些已经该为英文提示了.
server:
#include <time.h>;
#include <string.h>;
#include <netdb.h>;
#include <stdlib.h>;
#include <stdio.h>;
#include <sys/socket.h>;
#include <sys/types.h>;
#include <netinet/in.h>; /*sockaddr_in*/
#include <sys/types.h>;
#include <unistd.h>;
#define SEVRPOT 2400 /*服务监听端口号*/
#define MAXDATASIZE 100 /*每次最大数据传输量 */
#define BACKLOG 5 /*对队列中等待服务的请求的数目*/
//struct sockaddr_in{
// short int sin_family; /* 地址族 */
// unsigned short int sin_port; /* 端口号 */
// struct in_addr sin_addr; /* IP地址 */ 358750
// unsigned char sin_zero[8]; /* 填充0 以保持与struct sockaddr同样大小 */
//};
int main(int argc, char *argv[])
{
int sockfd,client_fd; /*sock_fd:监听socket;client_fd:数据传输socket */
struct sockaddr_in my_addr; /* 本机地址信息 */
struct sockaddr_in remote_addr; /* 客户端地址信息 */
int recvbytes;
int key; /* 取Esc的Asc码 */
char buf[MAXDATASIZE];
char ch;
char chr;
int pid;
if(( sockfd = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
{
printf( "创建SOCKET失败!!!" );
exit(1);
}
my_addr.sin_family = AF_INET; /* 协议 */
my_addr.sin_port = htons(SEVRPOT); /* 监听端口号 */
my_addr.sin_addr.s_addr = INADDR_ANY; /* 本机的IP地址 */
bzero(&(my_addr.sin_zero), ; /* 用bzero()函数将其置为零 */
/* 服务端通过调用bind函数来配置本地信息 */
if( bind( sockfd, ( struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1 )
{
printf( "bind 不成功!!!" );
exit(1);
}
/* 使socket处于被动的监听模式,并为该socket建立一个输入数据队列 */
if (listen(sockfd, BACKLOG) == -1)
{
printf("listen出错!" ;
exit(1);
}
while(1)
{
int sin_size;
sin_size = sizeof(struct sockaddr_in);
/*accept()函数让服务器接收客户的连接请求*/
if( (client_fd = accept( sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1)
{
printf( "接收客户的连接请求出错!!!" ;
continue;
}
printf("connect from %s\n",inet_ntoa(remote_addr.sin_addr));
pid = fork();
if (pid == -1)
{
printf("fork failure \n" ;
exit(1);
}
if (pid == 0 ){
if (send(client_fd,"hello, how do you do!",21,0)==-1)
{
printf("send failure!" ;
}
/* sockfd要改为client_fd?因为它现在是跟客户端沟通的通道.*/
while(1)
{
if ((recvbytes=recv(client_fd, &buf, MAXDATASIZE, 0)) >; 0)
{
buf[recvbytes] = '\0';
printf("recieve :%s\n",buf);
send(client_fd,"your data ,I accept!",21,0) ;
}
} //end of while(1)
} //end of if(pid == 0)
else
{
//process parent process;
setsid();
}
} //end of while(1)
}
================================================================
client:
#include <time.h>;
#include <string.h>;
#include <netdb.h>;
#include <stdlib.h>;
#include <stdio.h>;
#include <sys/socket.h>;
#include <sys/types.h>;
#include <netinet/in.h>; /*sockaddr_in*/
#define SERVPORT 2400
#define MAXDATASIZE 100 /*每次最大数据传输量 */
main(int argc, char *argv[])
{
int sockfd, recvbytes;
int client_fd;
int len;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
char chr[1024];
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("socket create failure" ;
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERVPORT);
serv_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(serv_addr.sin_zero), ;
if (connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(struct sockaddr)) == -1)
{
printf("connect failure" ;
exit(1);
}
for(; {
if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) ==-1)
{
printf("recieve failure\n" ;
exit(1);
}
buf[recvbytes]='\0';
printf("Recieve from server:%s\n",buf);
printf("please input command:\n" ;
scanf("%s",chr);
if ((send(sockfd, &chr, strlen(chr), 0)) == -1 )
{
printf("send failure!\n" ;
close(sockfd);
exit(0);
}
}
} |
|