客户机向服务器发送“Hello, I’m ***”服务器应答为“You are Welcome!”请用Socket实现,使用8888端

来源:百度知道 编辑:UC知道 时间:2024/06/19 13:02:37
急要
要写完整
谢谢

服务器:
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

#define LINE 2048

pthread_mutex_t work = PTHREAD_MUTEX_INITIALIZER;

struct sockaddr_in tcpsock, udpsock;

void *handle_tcp(void *arg)
{
int tcpfd = *((int *)arg);
int tcpcon;
int n;
char *suc = "success";
socklen_t len = 0;
struct sockaddr_in tcpcli;
listen(tcpfd, 20);
char str[INET_ADDRSTRLEN];
char buf[LINE];

while(1)
{
len = sizeof(tcpcli);
tcpcon = accept(tcpfd, (struct sockaddr*)&tcpcli, &len);
pthread_mutex_lock(&work);
printf("receive from %s at port %d\n",
inet_ntop(PF_INET, &tcpcli.sin_addr, str, sizeof(str)),
ntohs(tcpcli.sin_port));
pthread_mutex_unlock(&work);
while(1)
{<