对等通信是两台主机在通信时并不区分哪个是服务器请求方还是服务提供方,只要两台主机都运行了对等连接软件,就可以平等的,对等连接通信。
相同的地方就是都能得到想要的服务,只不过前者可能速度较慢。
客户端程序和服务器之间通信用这是网络传输层的问题,在传输层上主要就是两种数据包,即为TCP(可靠连接)、UDP(不可靠连接),这个其实与你想要做的服务器和程序设计没有大的关系,不知道你要搭建什么服务器,是windows还是linux 服务器,在windows下面一般是ASP.net +SQL server (很多人现在也在windows下做apacha+php+mysql); 在linux下面是apacha+php+mysql;网站使用的是HTTP协议来实现网站的建设//用fork的,也可用select/* server process */
/* include the necessary header files */
#include<ctype.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include <arpa/inet.h>
#include<stdio.h>
#include<unistd.h>
#define SIZE sizeof(struct sockaddr_in)
int newsockfd
int main (int argc, char ** argv) {
int sockfd
char c
/* set up the transport end point */
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
perror ("socket call failed")
exit (1)
}
/* initialize the internet socket with a port number of 7000
and the local address,specified as INADDR_ANY */
struct sockaddr_in server
server.sin_family=AF_INET
server.sin_addr.s_addr=INADDR_ANY
server.sin_port = htons(7000)
/* "bind server address to the end point */
if (bind (sockfd, (struct sockaddr *) &server, SIZE) == -1) {
perror ("bind call failed")
exit (1)
}
/* start listening for incoming connections */
if (listen (sockfd, 5) == -1) {
perror ("listen call failed")
exit (1)
}
for () {
/*accept a connection */
if ((newsockfd = accept (sockfd, NULL, NULL)) == -1) {
perror ("accept call failed")
continue
}
/* spawn a child to deal with the connection */
if (fork () == 0) {
while (recv (newsockfd, &c, 1, 0) >0) { // could use read
c = toupper (c)
send (newsockfd, &c, 1, 0) // could use write
}
/* when client is no longer sending information
the socket can be closed and the child process
terminated
*/
close (newsockfd)
exit (0)
}
close (newsockfd)
}
}
//客户端可用telnet
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)