新手入门
- 积分
- 5
- 金钱
- 5
- 注册时间
- 2016-5-19
- 在线时间
- 2 小时
|
楼主 |
发表于 2016-7-2 11:47:18
|
显示全部楼层
// TCP server
stop = RT_FALSE; /* ????*/
recv_data = rt_malloc(1024);
if (recv_data == RT_NULL)
{
rt_kprintf("No memory\n");
}
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
rt_kprintf("Socket error\n");
rt_free(recv_data);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080); /* ????????*/
server_addr.sin_addr.s_addr = INADDR_ANY;//htonl(0x0a3e2883);
rt_memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero));
if (bind(sock, (struct sockaddr *) &server_addr, sizeof(struct sockaddr))== -1)
{
rt_kprintf("Unable to bind\n");
rt_free(recv_data);
}
if (listen(sock, 5) == -1)
{
rt_kprintf("Listen error\n");
/* release recv buffer */
rt_free(recv_data);
}
rt_kprintf("\nTCPServer Waiting for client on port 8080...\n");
while (stop != RT_TRUE)
{
sin_size = sizeof(struct sockaddr_in);
connected = accept(sock, (struct sockaddr *) &client_addr, &sin_size);
rt_kprintf("I got a connection from (%s , %d)\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
后面的略
|
|