一些服务器客户端的c例子
今天早上6点起床之后练习的一些c的网络编程的基础例子
client1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
/* Make the necessary includes and set up the variables. */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int sockfd;
int len;
struct sockaddr_un address;
int result;
char ch = 'A' ;
/* Create a socket for the client. */
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
/* Name the socket, as agreed with the server. */
address.sun_family = AF_UNIX;
strcpy (address.sun_path, "server_socket" );
len = sizeof (address);
/* Now connect our socket to the server's socket. */
result = connect(sockfd, ( struct sockaddr *)&address, len);
if (result == -1) {
perror ( "oops: client1" );
exit (1);
}
/* We can now read/write via sockfd. */
write(sockfd, &ch, 1);
read(sockfd, &ch, 1);
printf ( "char from server = %c\n" , ch);
close(sockfd);
exit (0);
}
|
server1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
/* Make the necessary includes and set up the variables. */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_un server_address;
struct sockaddr_un client_address;
/* Remove any old socket and create an unnamed socket for the server. */
unlink( "server_socket" );
server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
/* Name the socket. */
server_address.sun_family = AF_UNIX;
strcpy (server_address.sun_path, "server_socket" );
server_len = sizeof (server_address);
bind(server_sockfd, ( struct sockaddr *)&server_address, server_len);
/* Create a connection queue and wait for clients. */
listen(server_sockfd, 5);
while (1) {
char ch;
printf ( "server waiting\n" );
/* Accept a connection. */
client_len = sizeof (client_address);
client_sockfd = accept(server_sockfd,
( struct sockaddr *)&client_address, &client_len);
/* We can now read/write to client on client_sockfd. */
read(client_sockfd, &ch, 1);
ch++;
write(client_sockfd, &ch, 1);
close(client_sockfd);
}
}
|
第二个服务器客户端的例子:
client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
/* Make the necessary includes and set up the variables. */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int sockfd;
int len;
struct sockaddr_in address;
int result;
char ch = 'A' ;
/* Create a socket for the client. */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
/* Name the socket, as agreed with the server. */
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr( "127.0.0.1" );
address.sin_port = htons(9734);
len = sizeof (address);
/* Now connect our socket to the server's socket. */
result = connect(sockfd, ( struct sockaddr *)&address, len);
if (result == -1) {
perror ( "oops: client3" );
exit (1);
}
/* We can now read/write via sockfd. */
write(sockfd, &ch, 1);
read(sockfd, &ch, 1);
printf ( "char from server = %c\n" , ch);
close(sockfd);
exit (0);
}
|
server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
/* Make the necessary includes and set up the variables. */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
/* Remove any old socket and create an unnamed socket for the server. */
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
/* Name the socket. */
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(9734);
server_len = sizeof (server_address);
bind(server_sockfd, ( struct sockaddr *)&server_address, server_len);
/* Create a connection queue and wait for clients. */
listen(server_sockfd, 5);
while (1) {
char ch;
printf ( "server waiting\n" );
/* Accept a connection. */
client_len = sizeof (client_address);
client_sockfd = accept(server_sockfd,
( struct sockaddr *)&client_address, &client_len);
/* We can now read/write to client on client_sockfd. */
read(client_sockfd, &ch, 1);
ch++;
write(client_sockfd, &ch, 1);
close(client_sockfd);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
/* As usual, make the appropriate includes and declare the variables. */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[])
{
char *host, **names, **addrs;
struct hostent *hostinfo;
/* Set the host in question to the argument supplied with the getname call,
or default to the user's machine. */
if (argc == 1) {
char myname[256];
gethostname(myname, 255);
host = myname;
}
else
host = argv[1];
/* Make the call to gethostbyname and report an error if no information is found. */
hostinfo = gethostbyname(host);
if (!hostinfo) {
fprintf (stderr, "cannot get info for host: %s\n" , host);
exit (1);
}
/* Display the hostname and any aliases it may have. */
printf ( "results for host %s:\n" , host);
printf ( "Name: %s\n" , hostinfo -> h_name);
printf ( "Aliases:" );
names = hostinfo -> h_aliases;
while (*names) {
printf ( " %s" , *names);
names++;
}
printf ( "\n" );
/* Warn and exit if the host in question isn't an IP host. */
if (hostinfo -> h_addrtype != AF_INET) {
fprintf (stderr, "not an IP host!\n" );
exit (1);
}
/* Otherwise, display the IP address(es). */
addrs = hostinfo -> h_addr_list;
while (*addrs) {
printf ( " %s" , inet_ntoa(*( struct in_addr *)*addrs));
addrs++;
}
printf ( "\n" );
exit (0);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
/* Start with the usual includes and declarations. */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main( int argc, char *argv[])
{
char *host;
int sockfd;
int len, result;
struct sockaddr_in address;
struct hostent *hostinfo;
struct servent *servinfo;
char buffer[128];
if (argc == 1)
host = "localhost" ;
else
host = argv[1];
/* Find the host address and report an error if none is found. */
hostinfo = gethostbyname(host);
if (!hostinfo) {
fprintf (stderr, "no host: %s\n" , host);
exit (1);
}
/* Check that the daytime service exists on the host. */
servinfo = getservbyname( "daytime" , "tcp" );
if (!servinfo) {
fprintf (stderr, "no daytime service\n" );
exit (1);
}
printf ( "daytime port is %d\n" , ntohs(servinfo -> s_port));
/* Create a socket. */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
/* Construct the address for use with connect... */
address.sin_family = AF_INET;
address.sin_port = servinfo -> s_port;
address.sin_addr = *( struct in_addr *)*hostinfo -> h_addr_list;
len = sizeof (address);
/* ...then connect and get the information. */
result = connect(sockfd, ( struct sockaddr *)&address, len);
if (result == -1) {
perror ( "oops: getdate" );
exit (1);
}
result = read(sockfd, buffer, sizeof (buffer));
buffer[result] = '\0' ;
printf ( "read %d bytes: %s" , result, buffer);
close(sockfd);
exit (0);
}
|
getdate-udp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
/* Start with the usual includes and declarations. */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main( int argc, char *argv[])
{
char *host;
int sockfd;
int len, result;
struct sockaddr_in address;
struct hostent *hostinfo;
struct servent *servinfo;
char buffer[128];
if (argc == 1)
host = "localhost" ;
else
host = argv[1];
/* Find the host address and report an error if none is found. */
hostinfo = gethostbyname(host);
if (!hostinfo) {
fprintf (stderr, "no host: %s\n" , host);
exit (1);
}
/* Check that the daytime service exists on the host. */
servinfo = getservbyname( "daytime" , "udp" );
if (!servinfo) {
fprintf (stderr, "no daytime service\n" );
exit (1);
}
printf ( "daytime port is %d\n" , ntohs(servinfo -> s_port));
/* Create a UDP socket. */
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
/* Construct the address for use with sendto/recvfrom... */
address.sin_family = AF_INET;
address.sin_port = servinfo -> s_port;
address.sin_addr = *( struct in_addr *)*hostinfo -> h_addr_list;
len = sizeof (address);
result = sendto(sockfd, buffer, 1, 0, ( struct sockaddr *)&address, len);
result = recvfrom(sockfd, buffer, sizeof (buffer), 0, ( struct sockaddr *)&address, &len);
buffer[result] = '\0' ;
printf ( "read %d bytes: %s" , result, buffer);
close(sockfd);
exit (0);
}
|
一些服务器客户端的c例子相关推荐
- TCP服务器和客户端的链接例子(侧重点在注意关闭套接子,减少套接子的描述子)
TCP服务器和客户端的链接例子(侧重点在注意关闭套接子,减少套接子的描述子) 每个文件或套接口都有一个访问计数,该访问计数在文件表项中维护,它表示当前指向该文件或套接口的打开的描述字个数. 每个文件, ...
- php运行socket服务器,PHP_php简单socket服务器客户端代码实例,本篇文章分享一个简单的socket - phpStudy...
php简单socket服务器客户端代码实例 本篇文章分享一个简单的socket示例,用php.实现一个接收输入字符串,处理并返回这个字符串到客户端的TCP服务. 产生一个 socket 服务端 /*文 ...
- UNIX TCP回射服务器/客户端之使用epoll模型的服务器
程序简介:这是一个运用epoll系列函数进行IO复用的服务器模型.它是目前UNIX与LINUX平台上效率最高,最受欢迎的IO复用传输模型. 其他的不说了,直接粘贴代码吧! 服务器端: #include ...
- linux tcp客户端端口号,Linux网络编程--服务器客户端(TCP实现)
Linux下的一个服务器客户端的小程序,基于TCP的实现:服务器可以同时接受多个客户的接入,通过子进程处理客户请求,下面的例子中,服务器只将客户的IP和端口以及发送的信息显示,然后原样的将客户发送的信 ...
- Android连接SQLServer详细教程(数据库+服务器+客户端),并在微软Azure云上搭建云服务
Android连接SQLServer详细教程(数据库+服务器+客户端),并在微软Azure云上搭建云服务 参考博客:http://blog.csdn.net/zhyl8157121/article/d ...
- socket 服务器浏览器与服务器客户端实例
一.服务器与浏览器 // 取得本机的loopback网络地址,即127.0.0.1 IPAddress address = IPAddress.Loopback; ...
- C#下如何实现服务器+客户端的聊天程序
C#下如何实现服务器+客户端的聊天程序 最近也在接触SOCKET编程,在当今这样一个网络时代,很多技术都以网络为中心在诞生,至少我认为是这样的,而SOCKET套接字接口,在实现网络通讯上处于关键地位, ...
- tcp和udp多线程的epoll服务器+客户端源代码 - brucema的个人空间 - 开源中国社区
tcp和udp多线程的epoll服务器+客户端源代码 - brucema的个人空间 - 开源中国社区 tcp和udp多线程的epoll服务器+客户端源代码
- stomp协议简介 服务器客户端通讯协议
一.STOMP协议介绍 STOMP即Simple (or Streaming) Text Orientated Messaging Protocol,简单(流)文本定向消息协议,它提供了一个可互操作的 ...
最新文章
- struts json序列化遇上replaceAll就出问题
- hdfs java api读写
- 关于车辆和车牌的检测相关文章
- 使用Visual Studio对项目重命名
- 使用labelme进行图片语义分割数据的标注(如何转换为训练的灰度图,即像素值为类别值)
- loadRunner分析指标
- atitit 点播系统 概览 v2 qb1.docx
- MAC安装Mysql超详细完整教程
- 代码整洁之道-程序员的职业素养
- 18年6月英语六级第一套听力单词
- C#进行注册表项和键值操作
- Python如何从列表中删除空列表?代码示例
- 丙二硫醇/鸟嘌呤(BG)/Mn配合物修饰BODIPY氟化硼二吡咯荧光探针
- 使用Termux在安卓手机上运行tomcat服务器
- Bootstrap 4 snippets 代码段
- 通信领域的dB计量单位
- 快速调整 图片的 像素大小
- 算法复杂度分析中的符号(Θ、Ο、ο、Ω、ω)的意义
- 积分换元法中换元单调性问题的讨论
- 直观说明Hadoop是什么?有什么作用?