TCP Server, implementing client Send message, server receiving message

Source: Internet
Author: User
Tags telnet program htons

Server.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <pthread.h>

static void usage (const char *PROC)
{
printf ("Usage:%s [IP] [port]\n", proc);
}

void *thread_run (void *arg)
{
printf ("Create a new thread\n");
int fd= (int) arg;
Char buf[1024];
while (1) {
memset (buf, ' n ', sizeof (BUF));
ssize_t _s=read (fd,buf,sizeof (BUF)-1);
if (_s>0) {
buf[_s]= ' + ';
printf ("client#%s\n", buf);
Write (Fd,buf,strlen (BUF));
}else if (_s==0) {
printf ("Client close...\n");
Break
}else{
printf ("Read error...\n");
Break
}
}
return (void *) 0;
}

int main (int argc,char *argv[])
{
if (argc!=3) {
Usage (argv[0]);
Exit (1);
}

1. Creat sock
int Listen_sock=socket (af_inet,sock_stream,0);
if (listen_sock<0) {
Perror ("socket");
return 1;
}

struct SOCKADDR_IN local;
Local.sin_family=af_inet;
Local.sin_port=htons (Atoi (argv[2)); //Port number
LOCAL.SIN_ADDR.S_ADDR=INET_ADDR (argv[1]); //IP Address

//2. Bind
if (Bind (Listen_sock, (struct sockaddr*) &local,sizeof (local)) <0) {
Perror ("bind");
return 2;
}

//3. Listen
Listen (listen_sock,5);

//4. Accept
struct SOCKADDR_IN peer;
Socklen_t len=sizeof (peer);
while (1) {
int Fd=accept (Listen_sock, (struct sockaddr*) &peer,&len);
if (fd<0) {
Perror ("accept");
return 3;
}
printf ("Get a new Link,socket-%s:&d\n", Inet_ntoa (PEER.SIN_ADDR), Ntohs (Peer.sin_port));

pthread_t ID;
Pthread_create (&id,null,thread_run, (void*) FD);

Pthread_detach (ID);
}
return 0;
}

Client.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <pthread.h>

static void usage (const char *PROC)
{
printf ("Usage:%s [IP] [port]\n", proc);
}

int main (int argc,char *argv[])
{
if (argc!=3) {
Usage (argv[0]);
Exit (1);
}
int Sock=socket (af_inet,sock_stream,0);
if (sock<0) {
Perror ("socket");
return 2;
}

struct SOCKADDR_IN remote;
Remote.sin_family=af_inet;
Remote.sin_port=htons (Atoi (argv[2)); ARGV[2]
REMOTE.SIN_ADDR.S_ADDR=INET_ADDR (argv[1]);//argv[1]

if (Connect (sock, struct sockaddr*) &remote,sizeof (remote)) <0) {
Perror ("Connect");
return 3;
}

Char buf[1024];
while (1) {
memset (buf, ' n ', sizeof (BUF));
printf ("Please Enter:");
Fflush (stdout);
ssize_t _s=read (0,buf,sizeof (BUF)-1);
if (_s>0) {
buf[_s-1]= ' + ';
Write (Sock,buf,strlen (BUF));
_s=read (sock,buf,sizeof (BUF));
if (_s>0) {
buf[_s]= ' + ';
printf ("%s\n", buf);
}
}
}
return 0;
}


Makefile File:

. Phony:all
All:client Server
Client:client.c
Gcc-o [email protected] $^-lpthread
Server:server.c
Gcc-o [email protected] $^-lpthread
. Phony:clean
Clean
RM-F Client Server

650) this.width=650; "width=" 467 "height=" 358 "title=" QQ picture 20160730110823.png "style=" width:380px;height:278px; "src= "Http://s3.51cto.com/wyfs02/M01/85/2E/wKiom1ecHdPStrloAABpNH-mLZY194.png" alt= " Wkiom1echdpstrloaabpnh-mlzy194.png "/>

Telnet is a remote terminal protocol

Use the Telnet program on the end user's computer to connect to the server. End users can enter commands in the Telnet program that run on the server as if they were entered directly on the server's console. The server can be controlled locally. To start a Telnet session, you must enter a user name and password to log on to the server. Telnet is a common way to remotely control a Web server.



TCP Server, implementing client Send message, server receiving message

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.