Socket Port Occupancy Command:
1. NETSTAT-ANPT
Now use the server side of C and the client side of Java for simple communication.
Server.c
/*
============================================================================
Name:server.c
Author:king
Version:
Copyright:your Copyright Notice
Description:hello World in C, Ansi-style
============================================================================
*/
#include <stdlib.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>/* INET (3) functions */
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
int handle (int point);
int main (void) {
int SFD, Ind;
struct sockaddr_in addr;
struct sockaddr_in clent;
Char resv[1024], sendbuf[1024];
Char buf[1024];
char * myaddr = "127.0.0.1";
int ret; return value settings
Socklen_t lent;
int pid;
addr.sin_family = af_inet; IPV4 Internet Protocols
Addr.sin_port = htons (5050); Enter the server port number here
ADDR.SIN_ADDR.S_ADDR = inet_addr (MYADDR);
; Inaddr_any indicates that the native IP
Obtain the socket descriptor, IPV4ASD
printf ("Socket start \ n");
SFD = socket (af_inet, sock_stream, 0);
if (SFD < 0) {
printf ("Socket error \ n");
return-1;
}
printf ("Bind start \ n");
Link a socket to a specified port
if (Bind (SFD, (struct sockaddr *) &addr, sizeof (struct sockaddr)) < 0) {
printf ("Bind error \ n");
return-1;
}
Listener sockets
printf ("Listen start \ n");
if (Listen (SFD, 1024x768) < 0) {
printf ("Listen error \ n");
return-1;
}
for (;;) {
Accept information from the client
printf ("Accept start \ n");
memset (&clent, 0, sizeof (clent));
lent = sizeof (clent);
IND = Accept (SFD, (struct sockaddr *) &clent, &lent);
if (Ind < 0) {
printf ("Accept error%d \ n", Ind);
return-1;
}
printf ("infor \ n");
printf ("Clent addr%s porit%d\n",
Inet_ntop (Af_inet, &clent.sin_addr, buf, sizeof (BUF)),
Ntohs (Clent.sin_port));
PID = fork ();
if (PID = = 0) {
Child process
Close (SFD);
HANDLE (IND);
} else if (PID < 0) {
Error
Close (Ind);
} else {
Parent process
}
}
return exit_success;
}
int handle (int point) {
int retn;
Char buf[1024];
for (;;) {
RETN = Read (point, buf, sizeof (BUF));
if (Retn < 0) {
printf ("read error \ n");
Close (point);
Break
} else if (Retn = = 0) {
printf ("Client exit \ n");
Close (point);
Break
}
printf ("client:%s\n", buf);
if (strcmp ("Exit", buf) = = 0) {
printf ("Exit \ n");
Close (point);
return 0;
}
}
return 0;
}
Client.c
Package HA. Socket;
Import java.io.*;
Import java.net.*;
public class Socketclient {
static Socket client;
Public Socketclient (String site, int port) {
try{
Client = new Socket (site,port);
System.out.println ("Client is created! Site: "+site+" Port: "+port";
}catch (Unknownhostexception e) {
E.printstacktrace ();
}catch (IOException e) {
E.printstacktrace ();
}
}
public string sendmsg (String msg) {
try{
BufferedReader in = new BufferedReader (New InputStreamReader (Client.getinputstream ()));
PrintWriter out = new PrintWriter (Client.getoutputstream ());
OUT.PRINTLN (msg);
Out.flush ();
return In.readline ();
}catch (IOException e) {
E.printstacktrace ();
}
Return "";
}
public void Closesocket () {
try{
Client.close ();
}catch (IOException e) {
E.printstacktrace ();
}
}
}
Package HA. Socket;
public class Testsocketclient {
public static void Main (string[] args) {
Socketclient client = new Socketclient ("127.0.0.1", 5050);
System.out.println (client.sendmsg ("test1111111111"));
Client.closesocket ();
}
}
Supplemental Note: Both the server side and the client use the same port number.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C and Java communicate through the socket