TCP服务器
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <time.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <errno.h>
#define LOG(s) printf("[%s] {%s:%d} %s\n", __DATE__, __FILE__, __LINE__, s);
int main(int argc, char *argv[])
{
int server = 0;
if((server = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
LOG("socket error");
return -1;
}
struct sockaddr_in saddr = {0};
saddr.sin_family = AF_INET;
saddr.sin_port = htons(8888);
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(server, (struct sockaddr*)&saddr, sizeof(saddr)) == -1)
{
LOG("bind error");
return -1;
}
if(listen(server, 5) == -1)
{
LOG("listen error");
return -1;
}
puts("server start success");
struct sockaddr_in caddr = {0};
int client = 0;
socklen_t asize = sizeof(caddr);
if((client = accept(server, (struct sockaddr*)&caddr, &asize)) == -1)
{
LOG("accept error");
return -1;
}
char buf[128] = " ";
while(1)
{
int len = 0;
bzero(buf, sizeof(buf));
if((len = recv(client, buf, sizeof(buf), 0)) < 0)
{
LOG("read error");
}
else if(len == 0)
{
break;
}
printf("[%s/%d]client: %s\n", inet_ntoa(caddr.sin_addr), ntohs(caddr.sin_port), buf);
bzero(buf, sizeof(buf));
strcpy(buf, "ok");
write(client, buf, len);
}
close(client);
close(server);
return 0;
}
TCP客户端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <time.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <errno.h>
#define LOG(s) printf("[%s] {%s:%d} %s \n", __DATE__, __FILE__, __LINE__, s);
int main(int argc, char *argv[])
{
int client = 0;
if((client = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
LOG("socket error");
return -1;
}
struct sockaddr_in caddr = {0};
caddr.sin_family = AF_INET;
caddr.sin_port = htons(8888);
caddr.sin_addr.s_addr = inet_addr("192.168.114.77");
socklen_t asize = sizeof(caddr);
if(connect(client, (struct sockaddr*)&caddr, asize) == -1)
{
LOG("connect error");
return -1;
}
printf("connect success\n");
char buf[128] = "";
while(1)
{
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
if(send(client, buf, sizeof(buf), 0) == -1)
{
LOG("send error");
return -1;
}
bzero(buf, sizeof(buf));
int len = 0;
if(((len = recv(client, buf, sizeof(buf), 0)) < 0))
{
LOG("recv error");
return -1;
}
else if(len == 0)
{
puts("server close");
}
printf("recv:\n");
printf("%s\n", buf);
}
close(client);
return 0;
}
UDP服务器
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <time.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <errno.h>
#define LOG(s) printf("[%s] {%s:%d} %s \n", __DATE__, __FILE__, __LINE__, s);
int main(int argc, char *argv[])
{
int server = 0;
if((server = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
LOG("socket");
return -1;
}
struct sockaddr_in saddr = {0};
saddr.sin_family = AF_INET;
saddr.sin_port = htons(8899);
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(server, (struct sockaddr*)&saddr, sizeof(saddr)) == -1)
{
LOG("bind error");
return -1;
}
puts("udp server start success");
char buf[128] = "";
socklen_t len = 0;
struct sockaddr_in remote = {0};
while(1)
{
bzero(buf, sizeof(buf));
int len = sizeof(remote);
int res = 0;
if((res = recvfrom(server, buf, sizeof(buf), 0, (struct sockaddr*)&remote, &len)) == -1)
{
LOG("recvfrom error");
return -1;
}
else if(res == 0)
{
break;
}
printf("[%s/%d] client: %s\n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port), buf);
bzero(buf, sizeof(buf));
strcpy(buf, "ok");
if(sendto(server, buf, sizeof(buf), 0, (struct sockaddr*)&remote, len) == -1)
{
LOG("sendto error");
return -1;
}
}
close(server);
return 0;
}
UDP客户端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <time.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <errno.h>
#define LOG(s) printf("[%s] {%s:%d} %s \n", __DATE__, __FILE__, __LINE__, s);
int main(int argc, char *argv[])
{
int client = 0;
if((client = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
LOG("socket error");
return -1;
}
struct sockaddr_in caddr = {0};
struct sockaddr_in remote = {0};
caddr.sin_family = AF_INET;
caddr.sin_port = htons(10086);
caddr.sin_addr.s_addr = htonl(INADDR_ANY);
remote.sin_family = AF_INET;
remote.sin_port = htons(8899);
remote.sin_addr.s_addr = inet_addr("192.168.114.227");
char buf[128] = "";
socklen_t len = sizeof(remote);
while(1)
{
int res = 0;
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
if(sendto(client, buf, sizeof(buf), 0, (struct sockaddr*)&remote, len) < 0)
{
LOG("sendto error");
return -1;
}
bzero(buf, sizeof(buf));
if((res = recvfrom(client, buf, sizeof(buf), 0, (struct sockaddr*)&remote, &len)) == -1)
{
LOG("recvfrom error");
return -1;
}
printf("recv: %s\n", buf);
}
close(client);
return 0;
}